Fit size to loaded FXML - javafx

I was trying to resolve my problem by many ways. But nothing works.
I have a MainApplicationWindowController
#FXML
private AnchorPane mainApplicationWindow;
#FXML
private AnchorPane workAnchorPane;
#FXML
private AnchorPane workAnchorPaneContent;
And corresponding fxml
<SplitPane dividerPositions="0.22690763052208834" layoutX="55.0" layoutY="46.0"
prefHeight="160.0" prefWidth="200.0" styleClass="gt-splitpane"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane id="splitPaneHorizontal" maxWidth="250.0" minWidth="250.0"
prefWidth="250.0">
<children>
<TableView fx:id="gtFamilyMemberTable" layoutX="20.0" layoutY="12.0"
onMouseClicked="#showInfoMember" prefHeight="200.0" prefWidth="200.0"
AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="10.0"
AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="5.0">
<columns>
<TableColumn fx:id="simNameColumn" maxWidth="110.0" minWidth="110.0"
prefWidth="-1.0" text="%simName"/>
<TableColumn fx:id="simSurnameColumn" maxWidth="110.0" minWidth="110.0"
prefWidth="-1.0" text="%simSurname"/>
</columns>
<styleClass>
<String fx:value="firstTypeTable"/>
<String fx:value="tableMembersAndRelations"/>
</styleClass>
</TableView>
<TableView fx:id="gtFamilyRelationTable" layoutX="20.0" layoutY="12.0"
onMouseClicked="#showInfoRelation" prefHeight="200.0"
prefWidth="200.0" AnchorPane.bottomAnchor="40.0"
AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0"
AnchorPane.topAnchor="5.0">
<columns>
<TableColumn fx:id="relationSimLeftColumn" maxWidth="85.0"
minWidth="85.0" prefWidth="-1.0" text="%relation_sim"/>
<TableColumn fx:id="relationTypeColumn" maxWidth="50.0" minWidth="50.0"
prefWidth="-1.0" text="%relation_type"/>
<TableColumn fx:id="relationSimRightColumn" maxWidth="85.0"
minWidth="85.0" prefWidth="-1.0" text="%relation_sim"/>
</columns>
<styleClass>
<String fx:value="firstTypeTable"/>
<String fx:value="tableMembersAndRelations"/>
</styleClass>
</TableView>
<ToggleButton fx:id="buttonShowMemberTable" layoutX="23.0"
mnemonicParsing="false" text="ToggleButton"
AnchorPane.bottomAnchor="10.0"/>
<ToggleButton fx:id="buttonShowRelationTable" layoutX="125.0"
mnemonicParsing="false" text="ToggleButton"
AnchorPane.bottomAnchor="10.0"/>
</children>
</AnchorPane>
<AnchorPane fx:id="workAnchorPane">
<children>
<AnchorPane fx:id="workAnchorPaneContent" styleClass="workingPane"
AnchorPane.bottomAnchor="40.0"
AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="20.0"
AnchorPane.topAnchor="5.0">
</AnchorPane>
</children>
</AnchorPane>
</items>
</SplitPane>
In workAnchorPaneContent I load another FXML using function from my SceenManager.class :
public FXMLPaneController loadFxml(FXMLPaneController controller, AnchorPane anchor, String fxml) {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml), this.context.getBundle());
try {
anchor.getChildren().clear();
anchor.getChildren().addAll((AnchorPane) loader.load());
controller = loader.getController();
controller.setManager(this);
controller.setContext(this.context);
} catch (Exception ex) {
LOG.error(ex.getClass() + " - " + ex.getMessage());
LOG.error(ex.getCause());
ex.printStackTrace();
}
return controller;
}
The problem is that after load FXML in workAnchorPaneContent the size is not fitted to size of workAnchorPane despite setting
AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="5.0"
#Edit
Jest I was trying to bind size properties. But it didn't works for AnchorPane inside second controller.

Related

Why TableView makes NullPointerException?

I have this controller class for showing the data of the method "getProduct" in a TableView, but I have a NullPointerException related to "tableViewItem.setItems(itemData)". I read some posts about this topic but didn't solve the problem.
package configuradordepc.controller;
//All imports
public class ControladorView implements Initializable {
#FXML private MenuItem mNew, mOpen, mSave, mPrint;
#FXML
private MenuItem mMother, mCPU, mRAM, mGPU, mHardDrive, mCase, mKey, mMouse,
mScreen, mSpeak, mMulti, mDVD, mFan, mPower;
#FXML private Button bSearch, bAdd, bRemove, bClean;
#FXML private TableView tableViewItem;
#FXML private TableColumn<Product, String> nameItemColumn;
#FXML private TableColumn<Product, Double> priceColumn;
#FXML private TableColumn<Product, Integer> availableColumn;
private ObservableList<Product> itemData;
#FXML private TableView tableViewBudget;
#FXML private TableColumn<Product, String> nameBudgetColumn;
#FXML private TableColumn<Product, Double> outTaxColumn;
#FXML private TableColumn<Product, Double> inTaxColumn;
#FXML private TableColumn<Product, Integer> quantityColumn;
private ObservableList<Product> budgetData;
#FXML private TextField tFSearch, tFMin, tFMax;
//Errors from TableView
#Override
public void initialize(URL location, ResourceBundle resources) {
//Items Table
nameItemColumn.setCellValueFactory(
new PropertyValueFactory<>("description"));
priceColumn.setCellValueFactory(
new PropertyValueFactory<>("price"));
availableColumn.setCellValueFactory(
new PropertyValueFactory<>("stock"));
itemData = FXCollections.observableArrayList();
getProduct();
tableViewItem.setItems(itemData);
//Budget Table
nameBudgetColumn.setCellValueFactory(
new PropertyValueFactory<>("description"));
inTaxColumn.setCellValueFactory(
new PropertyValueFactory<>("price"));
budgetData = FXCollections.observableArrayList();
tableViewBudget.setItems(budgetData);
//Bindings Item
final BooleanBinding noItemSelectedI = Bindings.isNull(
tableViewItem.getSelectionModel().selectedItemProperty());
final BooleanBinding emptySearch = Bindings.isEmpty(tFSearch.textProperty());
//Bindings Budget
final BooleanBinding noItemSelectedB = Bindings.isNull(
tableViewBudget.getSelectionModel().selectedItemProperty());
final BooleanBinding emptyBudget = Bindings.isEmpty(tableViewBudget.getItems());
//Disable botones
bSearch.disableProperty().bind(emptySearch);
bAdd.disableProperty().bind(noItemSelectedI);
bRemove.disableProperty().bind(noItemSelectedB);
bClean.disableProperty().bind(emptyBudget);
}
#FXML
private void onSearch(ActionEvent event) {
}
#FXML
private void onAdd(ActionEvent event) {
budgetData.add((Product) tableViewItem.getSelectionModel().selectedItemProperty().getValue());
}
#FXML
private void onDelete(ActionEvent event) {
}
#FXML
private void onClean(ActionEvent event) {
}
//Item Table initialization
public void getProduct() {
//SPEAKER
itemData.add(new Product("Logitech Z213 Multimedia Speakers 2.1", 22.95, 50, SPEAKER));
//HDD
itemData.add(new Product("Seagate Barracuda 7200.14 1TB SATA3", 46.95, 100, HDD));
//HDD_SSD
itemData.add(new Product("Samsung 850 Evo SSD Series 250GB SATA3", 82, 65, HDD_SSD));
//POWER_SUPPLY
itemData.add(new Product("Tacens Mars Gaming 700W", 42.75, 25, POWER_SUPPLY));
//DVD_WRITER
itemData.add(new Product("LG GH24NSD1 Grabadora DVD 24x Negra", 12.95, 55, DVD_WRITER));
//RAM
itemData.add(new Product("G.Skill Ripjaws X DDR3 1600 PC3-12800 8GB 2x4GB CL9", 37.95, 80, RAM));
//SCREEN
itemData.add(new Product("LG 22M47VQ-P 21.5 LED", 115, 10, SCREEN));
//MULTIREADER
itemData.add(new Product("Unotec Lector USB de tarjetas SD/MicroSD", 3.95, 110, MULTIREADER));
//MOTHERBOARD
itemData.add(new Product("Gigabyte GA-H81M-S2H", 49.95, 75, MOTHERBOARD));
//CPU
itemData.add(new Product("Intel Core i5-4460 3.2Ghz Box", 175, 490, CPU));
//MOUSE
itemData.add(new Product("Logitech Wireless Mouse M175 Negro", 12.95, 200, MOUSE));
//GPU
itemData.add(new Product("Gigabyte GeForce GTX 970 Gaming G1 WindForce OC 4GB GDDR5", 354, 480, GPU));
//KEYBOARD
itemData.add(new Product("Nox Krom Kombat Teclado + Ratón", 30.99, 54, KEYBOARD));
//CASE
itemData.add(new Product("NOX Sense 500W + Frontal", 41.95, 65, CASE));
//FAN
itemData.add(new Product("Cooler Master Hyper TX3 EVO CPU Cooler", 21.50, 220, FAN));
}
}
Here is the output:
ant -f C:\\Users\\Marco\\Desktop\\ConfiguradorDePC jfxsa-run
init:
Deleting: C:\Users\Marco\Desktop\ConfiguradorDePC\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\Marco\Desktop\ConfiguradorDePC\build\built-jar.properties
Compiling 2 source files to C:\Users\Marco\Desktop\ConfiguradorDePC\build\classes
Note: C:\Users\Marco\Desktop\ConfiguradorDePC\src\configuradordepc\controller\ControladorView.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
compile:
Deleting directory C:\Users\Marco\Desktop\ConfiguradorDePC\dist\lib
Copying 1 file to C:\Users\Marco\Desktop\ConfiguradorDePC\dist\lib
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_71\jre\..\lib\ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_71\jre\..\lib\ant-javafx.jar
No base JDK. Package will use system JRE.
No base JDK. Package will use system JRE.
jfx-deployment-script:
jfx-deployment:
jar:
Copying 13 files to C:\Users\Marco\Desktop\ConfiguradorDePC\dist\run150788111
jfx-project-run:
Executing C:\Users\Marco\Desktop\ConfiguradorDePC\dist\run150788111\ConfiguradorDePC.jar using platform C:\Program Files\Java\jdk1.8.0_71\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/Marco/Desktop/ConfiguradorDePC/dist/run150788111/ConfiguradorDePC.jar!/configuradordepc/view/View.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at configuradordepc.ConfiguradorDePC.start(ConfiguradorDePC.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.NullPointerException
at configuradordepc.controller.ControladorView.initialize(ControladorView.java:75)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
Exception running application configuradordepc.ConfiguradorDePC
Java Result: 1
Deleting directory C:\Users\Marco\Desktop\ConfiguradorDePC\dist\run150788111
jfxsa-run:
BUILD SUCCESSFUL (total time: 3 seconds)
The FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="configuradordepc.controller.ControladorView">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="Archivo">
<items>
<MenuItem fx:id="mNew" mnemonicParsing="false" text="Nuevo" />
<MenuItem fx:id="mOpen" mnemonicParsing="false" text="Abrir" />
<MenuItem fx:id="mSave" mnemonicParsing="false" text="Guardar" />
<MenuItem fx:id="mPrint" mnemonicParsing="false" text="Imprimir" />
</items>
</Menu>
</menus>
</MenuBar>
<Accordion VBox.vgrow="ALWAYS">
<panes>
<TitledPane animated="false" text="Nuevo PC">
<content>
<VBox fx:id="vBox" spacing="10.0">
<children>
<HBox spacing="10.0">
<children>
<TextField fx:id="tFSearch" HBox.hgrow="ALWAYS" />
<Button fx:id="bSearch" mnemonicParsing="false" onAction="#onSearch" text="Buscar" />
</children>
</HBox>
<HBox spacing="10.0" VBox.vgrow="ALWAYS">
<children>
<VBox spacing="10.0">
<children>
<MenuButton maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Categoría">
<items>
<MenuItem fx:id="mMother" mnemonicParsing="false" text="Placas base" />
<MenuItem fx:id="mCPU" mnemonicParsing="false" text="Procesadores" />
<MenuItem fx:id="mRAM" mnemonicParsing="false" text="Memorias RAM" />
<MenuItem fx:id="mGPU" mnemonicParsing="false" text="Tarjetas gráficas" />
<MenuItem fx:id="mHardDrive" mnemonicParsing="false" text="Discos duros" />
<MenuItem fx:id="mCase" mnemonicParsing="false" text="Torres" />
<MenuItem fx:id="mKey" mnemonicParsing="false" text="Teclados" />
<MenuItem fx:id="mMouse" mnemonicParsing="false" text="Ratones" />
<MenuItem fx:id="mScreen" mnemonicParsing="false" text="Monitores" />
<MenuItem fx:id="mSpeak" mnemonicParsing="false" text="Altavoces" />
<MenuItem fx:id="mMulti" mnemonicParsing="false" text="Multilectores" />
<MenuItem fx:id="mDVD" mnemonicParsing="false" text="Grabadoras" />
<MenuItem fx:id="mFan" mnemonicParsing="false" text="Ventiladores" />
<MenuItem fx:id="mPower" mnemonicParsing="false" text="Fuente de alimentación" />
</items>
</MenuButton>
<HBox alignment="CENTER_LEFT">
<children>
<Label prefWidth="87.0" text="Disponibilidad" />
<TextField fx:id="tFAvailable" HBox.hgrow="ALWAYS" />
</children>
</HBox>
<HBox alignment="CENTER_LEFT">
<children>
<Label prefWidth="87.0" text="Precio Mín." />
<TextField fx:id="tFMin" HBox.hgrow="ALWAYS" />
</children>
</HBox>
<HBox alignment="CENTER_LEFT">
<children>
<Label prefWidth="87.0" text="Precio Máx." />
<TextField fx:id="tFMax" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</VBox>
<VBox alignment="CENTER" spacing="10.0" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets />
</HBox.margin>
<children>
<TableView VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="nameItemColumn" editable="false" prefWidth="75.0" sortable="false" text="Nombre" />
<TableColumn fx:id="priceColumn" editable="false" prefWidth="75.0" sortable="false" text="Precio" />
<TableColumn fx:id="availableColumn" editable="false" prefWidth="75.0" sortable="false" text="Disponibilidad" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<Button fx:id="bAdd" mnemonicParsing="false" onAction="#onAdd" text="Añadir" />
</children>
</VBox>
<VBox spacing="10.0" HBox.hgrow="ALWAYS">
<children>
<TableView VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="nameBudgetColumn" editable="false" prefWidth="75.0" sortable="false" text="Nombre" />
<TableColumn fx:id="outTaxColumn" editable="false" prefWidth="75.0" sortable="false" text="Sin IVA" />
<TableColumn fx:id="inTaxColumn" editable="false" prefWidth="75.0" sortable="false" text="Con IVA" />
<TableColumn fx:id="quantityColumn" prefWidth="75.0" sortable="false" text="Cantidad" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<HBox alignment="CENTER" spacing="10.0">
<children>
<Button fx:id="bDelete" mnemonicParsing="false" onAction="#onDelete" text="Eliminar" />
<Button fx:id="bClean" mnemonicParsing="false" onAction="#onClean" text="Limpiar" />
</children>
</HBox>
</children>
</VBox>
</children>
</HBox>
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</VBox>
</content>
</TitledPane>
<TitledPane animated="false" text="Configuraciones" />
</panes>
</Accordion>
</children>
</VBox>
You are missing the fx:id attribute on the table. You need
<TableView fx:id="tableViewItem" VBox.vgrow="ALWAYS">
(or just add the fx:id to the table in the "code" section of Scene Builder, if you are using Scene Builder).

Table not updated with new row

I finally got the content into my table (because I've been mastering that mystery out for ages!). Now I'd like to add new Items to the list AND automatically update the table. I thought an ObservableList would do the trick but there seems to be more to it. Can you provide me with a solution?
Controller class:
package controller;
import java.lang.reflect.Constructor;
import db.ItemLijst;
import db.Klant;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import model.Item;
import model.ItemTypes;
public class SchermController {
#FXML
private TableView<Item> tblItems;
#FXML
private TableView<Klant> tblKlanten;
// #FXML
// private TableView<Uitlening> tblUitleningen;
#FXML
private Button btnItemToevoegen;
#FXML
private Button btnKlantToevoegen;
#FXML
private Button btnRegistreer;
#FXML
private ChoiceBox<ItemTypes> cbTypes;
#FXML
private ChoiceBox<Item> cbItems;
#FXML
private ChoiceBox<Klant> cbKlanten;
#FXML
private TextField tfTitel;
#FXML
private TextField tfVoornaam;
#FXML
private TextField tfAchternaam;
#FXML
private TextField tfStraat;
#FXML
private TextField tfNummer;
#FXML
private TextField tfPostcode;
#FXML
private TextField tfGemeente;
#FXML
private TableColumn<Item, String> tcID;
#FXML
private TableColumn<Item, ItemTypes> tcType;
#FXML
private TableColumn<Item, String> tcTitel;
#FXML
private TableColumn<Item, String> tcUitgeleend;
private ObservableList<Item> items = FXCollections.observableArrayList();
#FXML
private void initialize()
{
/*
* ItemLijst is a static object (or object with all static
methods). the CD, Film and Spel classes automatically add their record to
this class. Literally translated it means ItemList. I thought by making this
an ObservableList, the table would be automatically update its records
depending on a change to this variable (ItemLijst.items<Item>)?
*/
items = FXCollections.observableArrayList(ItemLijst.getItems());
tcID.setCellValueFactory(new PropertyValueFactory<Item, String>("ID"));
tcType.setCellValueFactory(new PropertyValueFactory<Item, ItemTypes>("Type"));
tcTitel.setCellValueFactory(new PropertyValueFactory<Item, String>("Titel"));
tcUitgeleend.setCellValueFactory(new PropertyValueFactory<Item, String>("UitgeleendString"));
tblItems.setItems(items);
// Item types
cbTypes.setItems(FXCollections.observableArrayList(ItemTypes.values()));
btnItemToevoegen.setOnAction(e -> {
try {
itemToevoegen();
} catch (Exception e1) {
System.out.println("Probleem: " + e1.getMessage());
}
});
}
/**
* This is the method which I use to add a new item to the CD, Film or Spel
class (which inherits the Item class).
*/
private void itemToevoegen() throws Exception
{
// Validatie
if (cbTypes.getValue() == null ) {
throw new Exception("Je moet een type kiezen");
} else if (tfTitel.getText().trim().isEmpty()) {
throw new Exception("Je moet een titel ingeven");
}
Class<?> klasse = Class.forName("model." + cbTypes.getValue().toString());
Constructor<?> cons = klasse.getConstructor(String.class);
cons.newInstance(tfTitel.getText());
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="438.0" prefWidth="743.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.SchermController">
<children>
<TabPane layoutX="-1.0" prefHeight="438.0" prefWidth="744.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab closable="false" text="Items">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="367.0" prefWidth="766.0">
<children>
<TableView fx:id="tblItems" layoutX="14.0" layoutY="14.0" prefHeight="343.0" prefWidth="714.0">
<columns>
<TableColumn fx:id="tcID" prefWidth="303.0" text="Item ID" />
<TableColumn fx:id="tcType" prefWidth="76.0" text="Type" />
<TableColumn fx:id="tcTitel" prefWidth="236.0" text="Titel" />
<TableColumn fx:id="tcUitgeleend" prefWidth="98.0" text="Uitgeleend" />
</columns>
</TableView>
<ChoiceBox fx:id="cbTypes" layoutX="23.0" layoutY="365.0" prefWidth="150.0" />
<TextField fx:id="tfTitel" layoutX="201.0" layoutY="365.0" prefHeight="26.0" prefWidth="352.0" promptText="Titel van item" />
<Button fx:id="btnItemToevoegen" layoutX="592.0" layoutY="365.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="131.0" text="Toevoegen" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab closable="false" text="Klanten">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="tblKlanten" layoutX="14.0" layoutY="14.0" prefHeight="343.0" prefWidth="714.0">
<columns>
<TableColumn prefWidth="89.0" text="Klant ID" />
<TableColumn prefWidth="87.0" text="Voornaam" />
<TableColumn prefWidth="105.0" text="Achternaam" />
<TableColumn prefWidth="154.0" text="Straat" />
<TableColumn prefWidth="54.0" text="Nr" />
<TableColumn prefWidth="96.0" text="Postcode" />
<TableColumn prefWidth="128.0" text="Gemeente" />
</columns>
</TableView>
<TextField fx:id="tfVoornaam" layoutX="14.0" layoutY="365.0" prefHeight="26.0" prefWidth="105.0" promptText="Voornaam" />
<TextField fx:id="tfAchternaam" layoutX="125.0" layoutY="365.0" prefHeight="26.0" prefWidth="94.0" promptText="Achternaam" />
<TextField fx:id="tfStraat" layoutX="243.0" layoutY="365.0" prefHeight="26.0" prefWidth="150.0" promptText="Straat" />
<TextField fx:id="tfNummer" layoutX="396.0" layoutY="365.0" prefHeight="26.0" prefWidth="32.0" promptText="Nr" />
<TextField fx:id="tfPostcode" layoutX="431.0" layoutY="365.0" prefHeight="26.0" prefWidth="60.0" promptText="Postcode" />
<TextField fx:id="tfGemeente" layoutX="494.0" layoutY="365.0" prefHeight="26.0" prefWidth="130.0" promptText="Gemeente" />
<Button fx:id="btnKlantToevoegen" layoutX="635.0" layoutY="365.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="93.0" text="Toevoegen" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab closable="false" text="Uitleningen">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="tblUitleningen" layoutX="14.0" layoutY="14.0" prefHeight="343.0" prefWidth="714.0">
<columns>
<TableColumn prefWidth="92.0" text="Klant ID" />
<TableColumn prefWidth="324.0" text="Item ID" />
<TableColumn prefWidth="155.0" text="Start Uitleen" />
<TableColumn prefWidth="142.0" text="Eind Uitleen" />
</columns>
</TableView>
<ChoiceBox fx:id="cbItems" layoutX="15.0" layoutY="364.0" prefHeight="26.0" prefWidth="185.0" />
<ChoiceBox fx:id="cbKlanten" layoutX="244.0" layoutY="364.0" prefHeight="26.0" prefWidth="203.0" />
<Button fx:id="btnRegistreer" layoutX="497.0" layoutY="364.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="229.0" text="Registreer Uitlening" />
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
Looking at the JavaDoc you will find:
public static <E> ObservableList<E> observableArrayList(Collection<? extends E> col)
Creates a new observable array list and adds a content of collection col to it.
So in fact, you are creating a new ObservableList and adding the initial items to it. The simplest solution would be to have ItemsLijst have an ObservableList, and return it in getItems. Then you should only have to do
tblItems.setItems(ItemsLijst.getItems()); // I corrected the class name

Add elements to a Stage from a different controller

I have a MainInterface.fxml, with its own controller MainController in which a button opens a new Stage from NewCrawler.fxml with its associated controller AddCrawlerController.
It looks fine, but i need to add stuff into the MainInterface using the interface NewCrawler .
The goal is adding a new "itemCrawler" each time someone press the "Start Crawling" button in NewCrawler, like the one hardcoded in MainInterface. How Can I do that?
MainController.java
public class MainController {
#FXML private AnchorPane mainPane;
#FXML
public void addCrawlerInstance(String domain) { //TODO dynamically add itemCrawler (Like the one hardcoded in MainInterface
Button b = new Button(domain); //it is only a try to add elements from a different controller
mainPane.getChildren().add(b);
}
#FXML
private void createCrawler () throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("guifxml/NewCrawler.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
}
AddCrawlerController.java
public class AddCrawlerController { //ADD Button from Main Interface
#FXML private Button abortButton;
#FXML private Button addCrawlerButton;
#FXML
private void abortCurrentWindow () {
Stage currentStage = (Stage) abortButton.getScene().getWindow();
currentStage.close();
}
#FXML
private void addNewCrawler () throws Exception { //TODO ref. Maincontroller.AddCrawlerInstance
FXMLLoader loader = new FXMLLoader(getClass().getResource("guifxml/MainInterface.fxml"));
Parent root = loader.load();
String d = "prova";
MainController controller = loader.getController();
controller.addCrawlerInstance("prova");
}
}
MainInterface.fxml
<VBox fx:id="mainApp" maxWidth="600.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="wsa.gui.MainController">
<children>
<ButtonBar maxWidth="643.0" nodeOrientation="RIGHT_TO_LEFT" prefHeight="40.0" prefWidth="643.0">
<buttons>
<Button alignment="CENTER" mnemonicParsing="false" text="Set Path" />
<Button alignment="CENTER" mnemonicParsing="false" text="Delete" />
<Button alignment="CENTER" mnemonicParsing="false" text="Stop" />
<Button alignment="CENTER" mnemonicParsing="false" text="Start" />
<Button alignment="CENTER" mnemonicParsing="false" onMouseClicked="#createCrawler" text="Add" />
</buttons>
<VBox.margin>
<Insets />
</VBox.margin>
<padding>
<Insets right="5.0" />
</padding>
</ButtonBar>
<AnchorPane fx:id="mainPane" maxHeight="-1.0" maxWidth="-1.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Separator layoutY="2.0" prefHeight="3.0" prefWidth="645.0" />
<Pane fx:id="itemCrawler" prefHeight="52.0" prefWidth="645.0">
<children>
<Text layoutX="44.0" layoutY="34.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Domain">
<font>
<Font size="20.0" />
</font>
</Text>
<TextField editable="false" layoutX="124.0" layoutY="13.0" prefHeight="25.0" prefWidth="319.0" text="http://www.prova.org" />
<Button alignment="CENTER" layoutX="456.0" layoutY="13.0" mnemonicParsing="false" text="Add URI" />
<Button alignment="CENTER" layoutX="528.0" layoutY="13.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="60.0" text="Explore" />
<CheckBox layoutX="14.0" layoutY="18.0" mnemonicParsing="false" prefHeight="17.0" prefWidth="17.0" />
<Separator layoutY="51.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="3.0" prefWidth="645.0" />
</children>
</Pane>
</children>
</AnchorPane>
</children>
</VBox>
NewCrawler.fxml
<AnchorPane fx:controller="wsa.gui.AddCrawlerController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TextArea layoutX="119.0" layoutY="89.0" prefHeight="237.0" prefWidth="467.0" />
<Text layoutX="44.0" layoutY="52.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Domain" wrappingWidth="74.13671875">
<font>
<Font size="20.0" />
</font>
</Text>
<TextField layoutX="119.0" layoutY="32.0" prefHeight="25.0" prefWidth="467.0" />
<Text layoutX="44.0" layoutY="111.0" strokeType="OUTSIDE" strokeWidth="0.0" text="URI list" textAlignment="CENTER" wrappingWidth="74.13672208786011">
<font>
<Font size="20.0" />
</font>
</Text>
<Text layoutX="119.0" layoutY="85.0" strokeType="OUTSIDE" strokeWidth="0.0" text="one URI on each line " textAlignment="CENTER" wrappingWidth="467.00000166893005" />
<Button fx:id="addCrawlerButton" layoutX="260.0" layoutY="353.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="115.0" onMouseClicked="#addNewCrawler" text="Start Crawling" />
<Button fx:id="abortButton" layoutX="118.0" layoutY="353.0" mnemonicParsing="false" onMouseClicked="#abortCurrentWindow" prefHeight="25.0" prefWidth="115.0" text="Abort" />
</children>
</AnchorPane>
MainInterface
NewCrawler
What I understand is that you need a dynamic list of itemCrawlers.
Instead of hardcoding a newCrawler, you should use a ListView with a custom CellFactory that constructs custom ListCell to represent itemCrawler.
This tutorial tells you how to do that : http://docs.oracle.com/javafx/2/ui_controls/list-view.htm
I am not 100% sure about this, so correct me if I am wrong.
In the NewCrawler's controller you load the FXML in its constructor. Then you can instantiate the NewCrawler controller and work with it as with any other object inside your MainController.

JavaFX: Empty window is displayed on first attempt

Preface: Newbie into Javafxml
My application is working perfectly. I just have one issue that I know should have a very simple solution, but I can't come up with anything more.
When i run my application, i get an empty window.
Empty Window
I have to resize the window in order to get the required
Resized Window
#FXML
void invoice(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/Invoice2.fxml"));
Parent planner;
try {
planner = (Parent) loader.load();
stage.setTitle("Order Details");
stage.getScene().setRoot(planner);
} catch (IOException e) {
e.printStackTrace();
}
}
FXML CODE:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<TitledPane collapsible="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1200.0" text="Invoice" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.InvoiceController">
<content>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<SplitPane dividerPositions="0.08355795148247978" layoutX="166.0" layoutY="108.0" orientation="VERTICAL" prefHeight="374.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" styleClass="body-default" stylesheets="#main.css" SplitPane.resizableWithParent="false">
<children>
<Button fx:id="back" layoutX="20.0" layoutY="14.0" mnemonicParsing="false" onAction="#BackButton" prefHeight="37.0" prefWidth="79.0" styleClass="btn-default" stylesheets="#main.css" text="Back" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="14.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<SplitPane dividerPositions="0.7078260869565217" layoutX="158.0" layoutY="29.0" prefHeight="283.0" prefWidth="577.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView fx:id="invoicelisttb1" layoutX="71.0" layoutY="14.0" prefHeight="281.0" prefWidth="404.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="order" prefWidth="148.0" text="Order No." />
<TableColumn fx:id="name" prefWidth="167.0" text="Name" />
<TableColumn fx:id="price" prefWidth="173.0" text="Price" />
</columns>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
<children>
<Label layoutX="14.0" layoutY="44.0" prefHeight="31.0" prefWidth="91.0" styleClass="bold" stylesheets="#main.css" text="Order No:" AnchorPane.rightAnchor="233.0" />
<TextField fx:id="txtorder" editable="false" layoutX="197.0" layoutY="44.0" prefHeight="25.0" prefWidth="83.0" AnchorPane.rightAnchor="58.0" />
<Label layoutX="14.0" layoutY="91.0" prefHeight="31.0" prefWidth="77.0" styleClass="bold" stylesheets="#main.css" text="Price:" AnchorPane.rightAnchor="246.0" />
<TextField fx:id="txtprice" layoutX="197.0" layoutY="91.0" prefHeight="25.0" prefWidth="83.0" AnchorPane.rightAnchor="58.0" />
<Button fx:id="btnprice" layoutX="196.0" layoutY="160.0" mnemonicParsing="false" onAction="#enterPrice" prefHeight="31.0" prefWidth="113.0" styleClass="btn-default" stylesheets="#main.css" text="Enter Price" AnchorPane.rightAnchor="58.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
</content>
</TitledPane>
After working on it for days, I finally solved it by simply putting the entire fxml's structure in another Anchor Pane. I don't know why that solved it or how that solved it, but when I wrapped the entire GUI in Anchor Pane, it started working fine.
Try the following:
try {
Parent planner = FXMLLoader.load(getClass().getResource("/view/Invoice2.fxml"));
stage.setTitle("Order Details");
stage.setScene(new Scene(planner));
} catch (IOException e) {
e.printStackTrace();
}
Instead of stage.getScene().setRoot(planner) I have used stage.setScene(new Scene(planner));.
Instead of loader.setLocation(getClass().getResource("/view/Invoice2.fxml")); change the resource to "../view/Invoice2.fxml". This will work if your project directory structure is something like this:
project
|
\_ view
| |
| \_ Invoice2.fxml
\_ main
\_ Main.java
The "../path" indicates that the selected path is in the same parent directory, not inside our current directory.

JavaFX read from text file and display in textarea

I am new to JavaFX and JavaFX Scene Builder and have been researching and trying to figure out for weeks now how to simply read from a text file and display its contents in a textarea. I have a good file reading function in my controller class, but I can't figure out how to display the text file to the text area in the fxml document. I've learned how to click a button and make the file display in the text area, but I want to have the contents in the text area as soon as the GUI loads. If anyone has an idea how to go about doing this, your help will be greatly appreciated!
The last button and text area (towards the end of the FXML document) is the button that prints my text file to netbeans, and the text area in which I would like the text to be dispalyed.
Here is my code:
Controller
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
public class Screen2Controller implements Initializable , ControlledScreen {
ScreensController myController;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
#FXML
private void goToScreen1(ActionEvent event){
myController.setScreen(ScreensFramework.screen1ID);
}
#FXML
private void goToProgram(ActionEvent event){
myController.setScreen(ScreensFramework.programID);
}
private void displayText() throws FileNotFoundException {
Scanner s = new Scanner (new File ("EECS.txt")).useDelimiter("\\s+");
while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
System.out.print(s.nextInt()+" "); // display the found integer
}
else {
System.out.print(s.next()+" "); // else read the next token
}
} //end while
} //end main
} //end screen2controller
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="650.0" prefWidth="1350.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="departmentdisplay.Screen2Controller">
<children>
<ImageView fitHeight="783.0" fitWidth="1398.0" layoutX="-4.0" layoutY="-80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#nexus_blue.jpg" />
</image>
</ImageView>
<ImageView fitHeight="149.0" fitWidth="1322.0" layoutX="20.0" layoutY="7.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#departinfohead.png" />
</image>
</ImageView>
<Button layoutX="49.0" layoutY="26.0" mnemonicParsing="false" onAction="#goToScreen1" prefHeight="111.0" prefWidth="121.0">
<graphic>
<ImageView fitHeight="100.0" fitWidth="150.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#backarrow.png" />
</image>
</ImageView>
</graphic></Button>
<SplitPane dividerPositions="0.20765027322404372" layoutX="928.0" layoutY="168.0" orientation="VERTICAL" prefHeight="185.0" prefWidth="415.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="95.0" prefWidth="183.0">
<children>
<Label layoutX="14.0" layoutY="23.0" prefHeight="96.0" prefWidth="158.0" text=" Map" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font name="Arial Black" size="24.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="176.0" prefWidth="158.0">
<children>
<TextArea prefHeight="151.0" prefWidth="242.0" text="Image" wrapText="true" AnchorPane.bottomAnchor="-7.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
</TextArea>
</children>
</AnchorPane>
</items>
</SplitPane>
<SplitPane dividerPositions="0.08057851239669421" layoutX="20.0" layoutY="168.0" orientation="VERTICAL" prefHeight="480.0" prefWidth="898.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Label layoutX="369.0" prefHeight="29.0" prefWidth="1062.0" text=" Electrical Engineering & Computer Science" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font name="Arial Black" size="24.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextArea fx:id="dinfoTextArea" layoutY="3.0" prefHeight="453.0" prefWidth="896.0"
text="Text file text goes here" wrapText="true"
AnchorPane.bottomAnchor="-14.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="3.0">
</TextArea>
</children>
</AnchorPane>
</items>
</SplitPane>
<SplitPane dividerPositions="0.13286713286713286" layoutX="930.0" layoutY="365.0" orientation="VERTICAL" prefHeight="282.0" prefWidth="413.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="50.0" prefWidth="221.0">
<children>
<Label layoutX="92.0" layoutY="12.0" prefHeight="29.0" prefWidth="263.0" text=" Programs" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font name="Arial Black" size="24.0" />
</font>
</Label>
<Button layoutX="25.0" mnemonicParsing="false" onAction="#displayText" prefHeight="34.0" prefWidth="102.0" text="Go To Programs" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="176.0" prefWidth="158.0">
<children>
<TextArea layoutX="57.0" layoutY="-6.0" prefHeight="339.0" prefWidth="240.0"
text="Lorem ipsum " wrapText="true" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
</TextArea>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
#FXML TextArea YourTextArea; //i think you already know about this
#Override
public void initialize(URL url, ResourceBundle rb) {
try {
Scanner s = new Scanner(new File("EECS.txt")).useDelimiter("\\s+");
while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
YourTextArea.appendText(s.nextInt() + " "); // display the found integer
} else {
YourTextArea.appendText(s.next() + " "); // else read the next token
}
}
} catch (FileNotFoundException ex) {
System.err.println(ex);
}
}
or you can simply call your button's 'on action' method from initialize(). eg.
#FXML public void displayTextOnButtonClick(){ //suppose this method gets fired when you click button
}
#FXML public void initialize(URL url, ResourceBundle rb) {
displayTextOnButtonClick();
}
Just put the code you wrote inside the displayTextOnButtonClick inside the initialize(). This is what initialize is meant for.
Initialize is called after processing the root node and it adds extra functionality(if defined) and data to the controls present in the fxml.

Resources