Skip to content

Instantly share code, notes, and snippets.

@dannyhvc
Last active September 11, 2023 16:42
Show Gist options
  • Save dannyhvc/31c6cdd082cc83174a18d786fee7ec93 to your computer and use it in GitHub Desktop.
Save dannyhvc/31c6cdd082cc83174a18d786fee7ec93 to your computer and use it in GitHub Desktop.
javafx in vscode and no build tools
// jfx imports
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
// java lib imports
import java.io.File;
import java.net.URL;
public class App extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
MyFxmlController controller = new MyFxmlController();
controller.setValue("New value");
loader.setController(controller);
File fxmlFile = new File("src\\assets\\fxml\\helloworld.fxml");
URL fxmlUrl = fxmlFile.toURI().toURL();
loader.setLocation(fxmlUrl);
VBox vbox = loader.<VBox>load();
// controller for fxml
MyFxmlController controllerRef = loader.getController();
// printing button text getters
System.out.println(controllerRef.getValue());
System.out.println(controllerRef.getLabel1Text());
System.out.println(controllerRef.getLabel2Text());
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<VBox xmlns:fx="http://javafx.com/fxml" spacing="20">
<children>
<Label fx:id="label1" text="Line 1"/>
<Label fx:id="label2" text="Line 2"/>
<Button fx:id="button1" text="Click me!" onAction="#buttonClicked"/>
</children>
</VBox>
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Launch App",
"request": "launch",
"mainClass": "App",
"projectName": "partnar_erp_dedc8781",
"vmArgs":
"--module-path ${workspaceFolder}/lib/javafx-sdk-17.0.1/lib --add-modules javafx.controls,javafx.fxml"
}
]
}
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class MyFxmlController {
private String value = "Default value";
public Label label1 = null;
public Label label2 = null;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public void initialize() {
System.out.println("Initialized MyFxmlController");
}
public String getLabel1Text() {
return this.label1.getText();
}
public String getLabel2Text() {
return this.label2.getText();
}
@FXML
public void buttonClicked(Event e){
System.out.println("Button clicked");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment