FXMLLoader getController returns NULL?

Change this @FXML public void editPerson() { try { FXMLLoader loader = new FXMLLoader(getClass().getResource( “PersonEditor.fxml”)); PersonEditorCtrl ctrl = loader.getController(); ctrl.init(table.getSelectionModel().getSelectedItem()); Parent root = (Parent) loader.load(); Scene newScene = new Scene(root); Stage newStage = new Stage(); newStage.setScene(newScene); newStage.show(); } catch (Exception e) { e.printStackTrace(); } } To that: @FXML public void editPerson() { try { FXMLLoader … Read more

getHostServices().showDocument() in a FXML File

You need to pass the HostServices to the Controller. Key Code: Set the HostServices in the Controller. HostServices hostServices ; public void setGetHostController(HostServices hostServices) { this.hostServices = hostServices; } Key Code: Passing HostServices to the Controller. FXMLLoader loader = new FXMLLoader(getClass().getResource(“FXMLDocument.fxml”)); Parent root = loader.load(); FXMLDocumentController fXMLDocumentController = loader.getController(); fXMLDocumentController.setGetHostController(getHostServices()); Main import javafx.application.Application; import javafx.fxml.FXMLLoader; … Read more

Localdate.format, format is not applied

Don’t format your date for insertion into your SQL database. Assuming that your database column has datatype date and you are using at least Java 8 and at least JDBC 4.2, just pass the LocalDate to your PreparedStatement as it is: PreparedStatement insertStmt = myConnection.prepareStatement( “insert into my_table(purchase_date) values (?)”); insertStmt.setObject(1, purchaseDate); Your JDBC driver … Read more

JavaFX – setVisible hides the element but doesn’t rearrange adjacent nodes

Node.setVisible(boolean) just toggles the visibility state of a Node. To exclude a Node from its parents layout calculations you additionally have to set its managed state, by calling Node.setManaged(false). If you want the managed state to be updated automatically alongside the visibility, you can use a binding as @jewelsea pointed out: node.managedProperty().bind(node.visibleProperty());

Customize ListView in JavaFX with FXML

I understand your question. There are mainly two ways to set items in a Listview: 1. Create the ObservableList and set the items of the ListView with the ObservableList (listView.setItems(observableList)). 2. Use the setCellFactory() method of the ListView class. You would prefer to use the setCellFactory() method, because this approach simplies the process as well … Read more

Grouping together JavaFX FXML Objects

You could use fx:define and fx:reference to place the elements in a List and in the scene graph and inject the list to the controller: <AnchorPane id=”AnchorPane” fx:id=”dashboard” prefHeight=”400.0″ prefWidth=”600.0″ stylesheets=”@css/dashboard.css” xmlns=”http://javafx.com/javafx/8″ xmlns:fx=”http://javafx.com/fxml/1″ fx:controller=”com.hassanalthaf.telemart.viewmodels.DashboardViewModel”> <fx:define> <!– create panes and store them in a list –> <ArrayList fx:id=”panes”> <AnchorPane fx:id=”home” layoutY=”29.0″ maxHeight=”-Infinity” maxWidth=”-Infinity” minHeight=”-Infinity” minWidth=”-Infinity” prefHeight=”371.0″ … Read more

How to use font awesome in a fxml project (javafx)

I achieved using FA Icons by adapting Jens Deters’s approach. His routines target dynamic gui composition opposing fxml’s declarative way. Nevertheless, his AwesomeIcon enumeration (which maps FA comprehensible names with unicode characters) suited perfectly to my intents. It should start by statically load the font in main/app class: public class App extends Application { static … Read more

How do I access a UI element from another controller class in JavaFX?

When you load the FXML for the secondary controller, do this: FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(getClass().getResource(“second.fxml”)); AnchorPane frame = fxmlLoader.load(); FXMLSecondaryController c = (FXMLSecondaryController) fxmlLoader.getController(); Then you can pass references to the second controller. This could just be the TextArea. EDIT: Replaced the load() call in the snippet above and added the setLocation() call. … Read more

One controller to 2 fxmls (JavaFX)

Yes, you can do this. Although, it can be done, I do not recommend this approach. Don’t place a fx:controller attribute in either FXML. Create a new controller and set the same controller into separate FXMLLoader instances. CustomerDialogController dialogController = new CustomerDialogController(param1, param2); FXMLLoader summaryloader = new FXMLLoader( getClass().getResource( “customerSummary.fxml” ) ); summaryLoader.setController(dialogController); Pane summaryPane … Read more

JavaFX Nested Controllers (FXML )

Thanks to Daniel (from OTN) I found the error in my code, the names of my controller variables were wrong. They should be <fx:id>Controller. In other words it should be: MainController.java public class MainController extends Controller { @FXML private Window dialog1; @FXML private DialogController dialog1Controller; @FXML private Window dialog2; @FXML private DialogController dialog2Controller; But studying … Read more