Why TableView makes NullPointerException? - javafx

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).

Related

Error in Setting Vbox inside Border pane Center on click of button

I am designing an javafx application using BorderPane as Rootlayout inside the border pane there is button on left. on Click of which i need to set the borderpane(rootlayout) center to a different addprodct.fxml . i want to add a vbox on Click on Insert Button inside Center of BorderPane.
I checked if my fxml is getting loaded properly or not with this
System.out.println("view folder: " + RootController.class.getResource("/Views/AddProduct.fxml")); its print exact path to fxml. i tried to Setting Stage with the new fxml to see if it loads properly and it worked properly.
FXMLLoader loader = new FXMLLoader();
System.out.println("view folder: " + RootController.class.getResource("/Views/AddProduct.fxml"));
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
tryvbox = loader.load();
main.getPrimaryStage().setScene(new Scene(tryvbox));
Yes, i know same question already have been asked but i implemented the solution given neither helped my problem.
Load new fxml in borderpane center
JavaFX: How to update the center view of a borderpane with new values
Main.java
package sample;
import Controllers.RootController;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
#FXML
private BorderPane rootLayout;
private Stage primaryStage;
#Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
mainwindow();
}
public void mainwindow() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/Views/Rootlayout.fxml"));
rootLayout = loader.load();
RootController rootController = loader.getController();
rootController.setMain(this);
Scene scene= new Scene(rootLayout);
primaryStage.setTitle("Inventory Manager");
primaryStage.setScene(scene);
primaryStage.show();
}
public BorderPane getRootLayout() {
return rootLayout;
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
RootController.java
package Controllers;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import sample.Main;
import java.io.IOException;
public class RootController {
#FXML
private Button rootBtnInsrt;
#FXML
private Button rootBtnUpdate;
#FXML
private BorderPane rootLayout;
private Main main;
public void setMain(Main main) {
this.main = main;
}
#FXML
void btnInsrtClick(MouseEvent event) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
try {
VBox addProduct1 = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
AddProductController addProductController= loader.getController();
addProductController.setMain(main);
main.getRootLayout().setCenter(rootLayout);
}
}
rootlayout.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane fx:id="rootLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.RootController">
<top>
<MenuBar prefHeight="21.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<bottom>
<AnchorPane prefHeight="29.0" prefWidth="600.0" style="-fx-background-color: Black;" BorderPane.alignment="CENTER" />
</bottom>
<left>
<VBox alignment="CENTER" prefHeight="345.0" prefWidth="164.0" spacing="20.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="rootBtnInsrt" mnemonicParsing="false" onMouseClicked="#btnInsrtClick" prefHeight="50.0" prefWidth="132.0" text="Insert " />
<Button fx:id="rootBtnUpdate" mnemonicParsing="false" onMouseClicked="#btnUpdateClick" prefHeight="50.0" prefWidth="135.0" text="Update" />
<Button mnemonicParsing="false" prefHeight="50.0" prefWidth="158.0" text="View" />
</children>
<padding>
<Insets left="10.0" right="10.0" />
</padding>
</VBox>
</left>
</BorderPane>
Addproduct.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="addproduct" alignment="CENTER" prefHeight="249.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label alignment="CENTER" text="Add Product" VBox.vgrow="ALWAYS" />
<GridPane VBox.vgrow="ALWAYS">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Product Code" />
<Label text="Product Name :" GridPane.rowIndex="1" />
<Label text="Product Price :" GridPane.rowIndex="2" />
<Label text="Product Quantity :" GridPane.rowIndex="3" />
<TextField GridPane.columnIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="3" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</GridPane>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Button alignment="CENTER" maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="ADD" HBox.hgrow="ALWAYS" />
<Button alignment="CENTER" maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Reset" HBox.hgrow="ALWAYS" />
<Button maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Back" HBox.hgrow="ALWAYS" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" />
</padding>
</HBox>
</children>
</VBox>
when tried running the following code i get error.
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
all in same error pane
this is my directory structure.
Directory Structure
So basically after all the work and help the problem arose due to the Try/Catch block.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
try {
VBox addProduct1 = loader.load();
}
catch (IOException e) {
e.printStackTrace();
}
i removed the try/catch and added exception to the method i.e void btnInsrtClick(MouseEvent event) throws IOException

Exception running application application

This is my fx class
package application;
import java.io.File;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("as1.fxml"));
primaryStage.setTitle("Abdul-Rahim University");
primaryStage.setScene(new Scene(root, 800, 500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
and this is as1.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.effect.GaussianBlur?>
<?import javafx.scene.effect.MotionBlur?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="abd">
<children>
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New" />
<MenuItem mnemonicParsing="false" text="Open…" />
<Menu mnemonicParsing="false" text="Open Recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As…" />
<MenuItem mnemonicParsing="false" text="Revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Preferences…" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Undo" />
<MenuItem mnemonicParsing="false" text="Redo" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Cut" />
<MenuItem mnemonicParsing="false" text="Copy" />
<MenuItem mnemonicParsing="false" text="Paste" />
<MenuItem mnemonicParsing="false" text="Delete" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Select All" />
<MenuItem mnemonicParsing="false" text="Unselect All" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About MyHelloApp" />
</items>
</Menu>
</menus>
</MenuBar>
<AnchorPane fx:id="ar" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<BorderPane fx:id="bo" layoutX="69.0" layoutY="32.0" prefHeight="200.0" prefWidth="200.0" />
<HBox fx:id="hb" focusTraversable="true" layoutX="70.0" layoutY="128.0" prefHeight="100.0" prefWidth="420.0" spacing="10.0">
<children>
<Button fx:id="st" mnemonicParsing="false" onAction="#handleButtonAction" text="Students" />
<Button fx:id="ac" mnemonicParsing="false" onAction="#handleButtonAction2" text="Academics" />
<Button fx:id="co" mnemonicParsing="false" onAction="#handleButtonAction3" text="Courses" />
</children>
<opaqueInsets>
<Insets bottom="11.0" left="11.0" right="11.0" top="10.0" />
</opaqueInsets>
<effect>
<GaussianBlur radius="1.75" />
</effect>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
<Label fx:id="l1" contentDisplay="BOTTOM" layoutX="236.0" layoutY="26.0" text="University" textAlignment="CENTER" textOverrun="WORD_ELLIPSIS">
<font>
<Font name="Dubai Regular" size="33.0" />
</font>
<effect>
<MotionBlur radius="5.5" />
</effect>
</Label>
</children>
</AnchorPane>
</children>
</VBox>
I have tried to solve it alot and i cant if anyone can help me please solve this problem to me Exception running application applicatio i have tried to solve it alot and i cant if anyone can help me please solve this problem to me Exception running application applicatio
eclipse
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source) Caused by: javafx.fxml.LoadException:
/C:/Users/Abdurrahim/Desktop/mamoun/GUI/bin/application/as1.fxml:18
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
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 application.Main.start(Main.java:18)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.ClassNotFoundException: abd
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 22 more Exception running application application.Main
my controller
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
public class FXMLDocumentController {
#FXML
private AnchorPane ar;
#FXML
private BorderPane bo;
#FXML
private HBox hb;
#FXML
private Button st;
#FXML
private Button ac;
#FXML
private Button co;
#FXML
private Label l1;
#FXML
void handleButtonAction(ActionEvent event) {
System.out.println("abd");
}
#FXML
void handleButtonAction2(ActionEvent event) {
System.out.println("abd");
}
#FXML
void handleButtonAction3(ActionEvent event) {
System.out.println("abd");
}
}
javafx
Here is the solution: fx:controller="application.FXMLDocumentController"

Fit size to loaded FXML

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.

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.

Resources