JavaFX read from text file and display in textarea - javafx

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.

Related

JavaFX ScrollPane and FlowPane properties to resize the FlowPane if columns overfill the ScrollPane width

I have a FlowPane wrapped in a ScrollPane. FlowPane orientation is Vertical, so it will wrap the controls. But I want to set the FlowPane to resize vertically if the columns size is greater than the width of ScrollPane. I've tried a lot of settings, both on ScrollPane and FlowPane but none of them helped me with my wish.
As an image of how I want to do is something like this:
(red contur is ScrollPane, green is FlowPane)
Containers, after the flow pane is populated, with ScrollPane's width more than two columns of controls:
How it works right now, after resizing:
How I want to do after resizing the ScrollPane:
Can this be achieved? What settings I must do to both ScrollPane and FlowPane?
Edit:
Minimal reproduction code:
hello-view.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="424.0" prefWidth="457.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.demo.HelloController">
<children>
<AnchorPane layoutX="14.0" layoutY="14.0" prefHeight="399.0" prefWidth="430.0" style="-fx-border-color: #555555;" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
<children>
<ScrollPane fitToHeight="true" fitToWidth="true" focusTraversable="false" hbarPolicy="NEVER" layoutX="14.0" layoutY="13.0" maxWidth="1.7976931348623157E308" prefHeight="377.0" prefWidth="404.0" style="-fx-border-color: red; -fx-border-width: 2;" AnchorPane.bottomAnchor="7.0" AnchorPane.leftAnchor="13.0" AnchorPane.rightAnchor="12.0" AnchorPane.topAnchor="12.0">
<content>
<FlowPane maxWidth="1.7976931348623157E308" orientation="VERTICAL" prefHeight="363.0" prefWidth="397.0" rowValignment="TOP" style="-fx-border-color: green; -fx-border-width: 2;">
<children>
<Button mnemonicParsing="false" prefHeight="121.0" prefWidth="140.0" text="1">
<FlowPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</FlowPane.margin>
<font>
<Font name="System Bold" size="24.0" />
</font>
</Button>
<Button layoutX="12.0" layoutY="12.0" mnemonicParsing="false" prefHeight="121.0" prefWidth="140.0" text="2">
<FlowPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</FlowPane.margin>
<font>
<Font name="System Bold" size="24.0" />
</font>
</Button>
<Button layoutX="10.0" layoutY="135.0" mnemonicParsing="false" prefHeight="121.0" prefWidth="140.0" text="3">
<FlowPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</FlowPane.margin>
<font>
<Font name="System Bold" size="24.0" />
</font>
</Button>
<Button layoutX="154.0" layoutY="10.0" mnemonicParsing="false" prefHeight="121.0" prefWidth="140.0" text="4">
<FlowPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</FlowPane.margin>
<font>
<Font name="System Bold" size="24.0" />
</font>
</Button>
</children>
</FlowPane>
</content>
</ScrollPane>
</children>
</AnchorPane>
</children>
</AnchorPane>
HelloController.java:
package com.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class HelloController {
#FXML
private Label welcomeText;
#FXML
protected void onHelloButtonClick() {
welcomeText.setText("Welcome to JavaFX Application!");
}
}
HelloApplication.java
package com.example.demo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
#Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
HelloApplication.java and HelloController.java are default demo files from starting project in JavaFX.
Conclusion:
Is there a combination of properties for ScrollPane and FlowPane to be able to resize the FlowPane vertically and stop resizing in the right when the control inside tries to move to create a new column (this happens when ScrollPane resize vertically)? I don't want to create those invisible columns in the right, instead grows the FlowPane vertically!
Mention: this could happens in two way
when resize form from the bottom, and the controls from the bottom of the FlowPane will move to the top, and the FlowPane will resize automatically to the right and put the controls in the hidden area of FlowPane, (and)
When you resize form horizontally and there is no more space to move the controls from the right column to the next row, so the FlowPane will not stay anchored to right and to try to create an "invisible" row (or as many as it takes to move needed controls down).
I hope I make it clear as possible.
This answer I found it myself and it works only for FlowPane containing controls with the same size.
Purpose:
Allow user to populate a FlowPane in Vertical orientation, with parent ScrollPane's scrollbars set only to vertical scrollbar visible (when needed) and the FlowPane width to fit ScrollPane's width always.
Starting from a demo project in JavaFX, this is the fxml file which contains definition for the main-view (hello-view.fxml in this case).
hello-view.fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="424.0" prefWidth="457.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.demo.HelloController">
<children>
<AnchorPane layoutX="14.0" layoutY="14.0" prefHeight="399.0" prefWidth="430.0" style="-fx-border-color: #555555;" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
<children>
<ScrollPane fx:id="scrollPane" fitToWidth="true" focusTraversable="false" hbarPolicy="NEVER" layoutX="14.0" layoutY="13.0" maxHeight="1.7976931348623157E308" maxWidth="-Infinity" prefHeight="377.0" prefWidth="404.0" style="-fx-border-color: red; -fx-border-width: 0;" AnchorPane.bottomAnchor="7.0" AnchorPane.leftAnchor="13.0" AnchorPane.rightAnchor="12.0" AnchorPane.topAnchor="12.0">
<content>
<AnchorPane fx:id="ancFlow">
<children>
<FlowPane fx:id="flowPane" orientation="VERTICAL" prefHeight="368.0" prefWidth="396.0" prefWrapLength="10.0" rowValignment="TOP" style="-fx-border-color: green; -fx-border-width: 0;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Button maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="121.0" prefWidth="140.0" text="1">
<FlowPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</FlowPane.margin>
<font>
<Font name="System Bold" size="24.0" />
</font>
</Button>
<Button layoutX="12.0" layoutY="12.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="121.0" prefWidth="140.0" text="2">
<FlowPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</FlowPane.margin>
<font>
<Font name="System Bold" size="24.0" />
</font>
</Button>
<Button layoutX="10.0" layoutY="135.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="121.0" prefWidth="140.0" text="3">
<FlowPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</FlowPane.margin>
<font>
<Font name="System Bold" size="24.0" />
</font>
</Button>
<Button layoutX="154.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="121.0" prefWidth="140.0" text="4">
<FlowPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</FlowPane.margin>
<font>
<Font name="System Bold" size="24.0" />
</font>
</Button>
</children>
</FlowPane>
</children>
</AnchorPane>
</content>
</ScrollPane>
</children>
</AnchorPane>
</children>
</AnchorPane>
HelloController.java file:
package com.example.demo;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane;
import java.net.URL;
import java.util.ResourceBundle;
public class HelloController implements Initializable {
private final long buttonWidth = 144;
private final long buttonHeight = 125;
private double columns;
private double rows;
#FXML
private AnchorPane ancFlow;
#FXML
private ScrollPane scrollPane;
#FXML
private FlowPane flowPane;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// next line was used to see how ancPane is resizing
//ancFlow.setBackground(new Background(new BackgroundFill(Color.rgb(220, 120, 120), new CornerRadii(0), new Insets(0))));
}
public void resizeFlowPaneParent(){
scrollPane.heightProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
if(scrollPane.getHeight() < buttonHeight) return;
double verticalPadding = scrollPane.getPadding().getTop() + scrollPane.getPadding().getTop();
ancFlow.setPrefWidth(scrollPane.getWidth() - (scrollPane.getWidth() - scrollPane.getViewportBounds().getWidth()));
int controls = flowPane.getChildren().size();
rows = Math.floorDiv(newValue.longValue() , buttonHeight + (long)verticalPadding);
if((long)rows == 0) return;
columns = Math.ceilDiv(controls, (long)(rows));
double matchColumns = Math.floorDiv((long)(scrollPane.getWidth()- (scrollPane.getWidth() - scrollPane.getViewportBounds().getWidth())), buttonWidth);
if (columns <= matchColumns) {
if(rows * buttonHeight - (long)verticalPadding <= scrollPane.getHeight() - verticalPadding || (rows * columns * buttonHeight ) > controls * buttonHeight )
{
ancFlow.setPrefHeight(scrollPane.getHeight()-verticalPadding);
}
else
{
ancFlow.setPrefHeight(rows * buttonHeight);
}
}
else
{
double matchRows = Math.ceilDiv(controls, (long)matchColumns);
ancFlow.setPrefHeight(matchRows * buttonHeight);
}
}
});
scrollPane.widthProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
if(newValue.longValue() - (newValue.longValue() - scrollPane.getViewportBounds().getWidth()) < buttonWidth) return;
double verticalPadding = scrollPane.getPadding().getTop() + scrollPane.getPadding().getTop();
ancFlow.setPrefWidth(newValue.longValue() - (newValue.longValue() - scrollPane.getViewportBounds().getWidth()));
int controls = flowPane.getChildren().size();
columns = Math.floorDiv(newValue.longValue() - (long)(newValue.longValue() - scrollPane.getViewportBounds().getWidth()), buttonWidth);
rows = Math.ceilDiv(controls, (long)columns);
if(rows * buttonHeight < scrollPane.getHeight())
{
ancFlow.setPrefHeight(scrollPane.getHeight()-verticalPadding);
}
else
{
ancFlow.setPrefHeight(rows * buttonHeight);
}
}
});
}
}
HelloApplication.java file:
package com.example.demo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
#Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
((HelloController)fxmlLoader.getController()).resizeFlowPaneParent();
stage.setWidth(stage.getWidth() + 1);
stage.setHeight(stage.getHeight() + 1);
}
public static void main(String[] args) {
launch();
}
}
The approach was to wrap the FlowPane in a AnchorPane. FlowPane is anchored in AnchorPane to all bounds. The AnchorPane is wrapped in ScrollPane.
In controller I've created a public method in which I added listners to widthProperty() and heightProperty() of ScrollPane to be able to calculate the rows needed for resizing the AnchorPane, based on how many items are in FlowPane and how many columns could be made with the ScrollPane's width. This method was added because size of controls must be read after the scene is displayed, which I've done it in HelloApplication.java main class. In this way listners are added after all items are displayed, and it will be calculated correctly.
Demo:

Why javafx program is not running when i use fontawesomefx icons in fxml [duplicate]

This question already has an answer here:
javaFX program not working after changing the scene builder and jdk
(1 answer)
Closed 4 months ago.
I am new to javafx.Currently I am developing a miniproject.I am designing it's UI in scene builder-18.0.0.
I have added fontawesomefx jar file to reference library.
When I use fontawesomefx icons to my project, it's shows the following Exception.If I remove the icons, the program runs fine.
Result
Exception in Application start method
java.lang.reflect.InvocationTargetExceptionf
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at javafx.graphics#18.0.2/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics#18.0.2/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics#18.0.2/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics#18.0.2/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NoClassDefFoundError: com/sun/javafx/css/parser/CSSParser
at de.jensd.fx.glyphs.GlyphIcon.<clinit>(GlyphIcon.java:49)
at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unsafe.java:1160)
at java.base/jdk.internal.reflect.MethodHandleAccessorFactory.ensureClassInitialized(MethodHandleAccessorFactory.java:300)
at java.base/jdk.internal.reflect.MethodHandleAccessorFactory.newConstructorAccessor(MethodHandleAccessorFactory.java:103)
at java.base/jdk.internal.reflect.ReflectionFactory.newConstructorAccessor(ReflectionFactory.java:236)
at java.base/java.lang.reflect.Constructor.acquireConstructorAccessor(Constructor.java:546)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:496)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
at javafx.fxml#18.0.2/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1020)
at javafx.fxml#18.0.2/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:757)
at javafx.fxml#18.0.2/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2808)
at javafx.fxml#18.0.2/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2634)
at javafx.fxml#18.0.2/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml#18.0.2/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
at App.start(App.java:19)
at javafx.graphics#18.0.2/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics#18.0.2/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics#18.0.2/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics#18.0.2/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics#18.0.2/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics#18.0.2/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics#18.0.2/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Caused by: java.lang.ClassNotFoundException: com.sun.javafx.css.parser.CSSParser
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 25 more
Exception running application App
My Fxml code
<?xml version="1.0" encoding="UTF-8"?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="609.0" prefWidth="932.0" styleClass="holder" stylesheets="#TableStyle.css" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Pane layoutY="7.0" prefHeight="28.0" prefWidth="932.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label layoutX="17.75" layoutY="3.0" styleClass="app-title" text="Hello Tables" textFill="WHITE">
<font>
<Font size="15.0" />
</font>
</Label>
</children>
</Pane>
<AnchorPane layoutX="14.0" layoutY="28.0" prefHeight="200.0" prefWidth="256.0" styleClass="sideview" AnchorPane.bottomAnchor="7.0" AnchorPane.leftAnchor="7.0" AnchorPane.topAnchor="28.0">
<children>
<StackPane alignment="CENTER_RIGHT" layoutX="17.0" layoutY="48.0" AnchorPane.leftAnchor="35.6" AnchorPane.topAnchor="45.0">
<children>
<TextField promptText="Search here" styleClass="txt-field" />
</children>
</StackPane>
<ListView layoutX="28.0" layoutY="131.0" prefHeight="200.0" prefWidth="200.0" styleClass="listview" AnchorPane.bottomAnchor="110.0" AnchorPane.topAnchor="125.0" />
<StackPane layoutX="17.0" layoutY="514.0" prefHeight="30.0" prefWidth="222.0" AnchorPane.bottomAnchor="29.600000000000023">
<children>
<Button maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="145.0" styleClass="btn-new-table" />
<Label styleClass="txt-new-table" text="NEW TABLE" />
</children>
</StackPane>
<FontAwesomeIconView layoutX="111.0" layoutY="293.0" />
</children>
</AnchorPane>
<AnchorPane layoutX="316.0" layoutY="38.0" prefHeight="200.0" prefWidth="200.0" styleClass="sideview" AnchorPane.bottomAnchor="7.0" AnchorPane.leftAnchor="267.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="28.0">
<children>
<Label layoutX="27.0" layoutY="69.0" text="NOTES" AnchorPane.leftAnchor="27.0" AnchorPane.topAnchor="69.0">
<font>
<Font name="System Bold" size="21.0" />
</font>
</Label>
<StackPane layoutX="27.0" layoutY="126.0" prefHeight="353.0" prefWidth="611.0" AnchorPane.bottomAnchor="96.0" AnchorPane.leftAnchor="27.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="125.0">
<children>
<Label alignment="TOP_LEFT" prefHeight="75.0" prefWidth="214.0" styleClass="txt-no-table" text="NO TABLE SELECTED" textAlignment="CENTER" StackPane.alignment="CENTER">
<font>
<Font name="Roboto" size="16.799999237060547" />
</font>
</Label>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Pane layoutY="41.0" prefHeight="200.0" prefWidth="200.0" styleClass="table-holder" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="106.5" AnchorPane.topAnchor="2.0" />
<TableView prefHeight="353.0" prefWidth="503.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="95.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="NAME" />
<TableColumn prefWidth="75.0" text="AGE" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<VBox alignment="CENTER" layoutX="556.0" layoutY="27.0" prefHeight="327.0" prefWidth="48.0" AnchorPane.bottomAnchor="27.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="0.0">
<children>
<FlowPane alignment="CENTER" orientation="VERTICAL" prefHeight="315.0" prefWidth="55.0" styleClass="toolbar" />
</children>
</VBox>
</children>
</AnchorPane>
</children></StackPane>
<FlowPane alignment="CENTER_LEFT" hgap="10.0" layoutX="27.0" layoutY="432.0" prefHeight="102.0" prefWidth="490.0" vgap="10.0" AnchorPane.bottomAnchor="1.0" AnchorPane.leftAnchor="27.0" AnchorPane.rightAnchor="141.0">
<children>
<TextField promptText="Search here" styleClass="txt-field" />
</children>
</FlowPane>
<StackPane layoutX="422.0" layoutY="499.0" maxHeight="-Infinity" prefHeight="30.0" prefWidth="153.0" AnchorPane.bottomAnchor="30.0" AnchorPane.rightAnchor="120.0">
<children>
<Button maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="131.0" styleClass="btn-new-table" />
<Label styleClass="txt-new-table" text="ADD ROW" />
</children>
</StackPane>
</children>
</AnchorPane>
</children>
</AnchorPane>
Java(Main class)
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class App extends Application{
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("TableAppInterface.fxml"));
System.out.println("hi");
Parent root = null;
try {
root = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(root);
stage.setScene(scene);
//stage.initStyle(StageStyle.TRANSPARENT);
stage.setTitle("Tables");
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Launch App",
"request": "launch",
"mainClass": "App",
"projectName": "TableApp_e71931e0",
"vmArgs": "--module-path \"C:/Program Files/Java/javafx-sdk-18.0.2/lib\" --add-modules javafx.controls,javafx.fxml"
}
]
}
I also tried with the following vmArgs
"vmArgs": "--module-path \"C:/Program Files/Java/javafx-sdk-18.0.2/lib\" --add-modules javafx.controls,javafx.fxml \ --add-opens javafx.graphics/javafx.css=ALL-UNNAMED"
I also visited this post and used fontawesomfx 8.4.but it didn't work.It shows the same exception.
Currently I am using java 18(jdk-18.0.2) and javafx-sdk-18.0.2 and SceneBuilder-18.0.0 and fontawesomefx 8.9 jar.I use visual studio code.I am not using any built tools for my project.
NOTE:If I run the code without any fontawesomefx icons, my code works fine.
I have been searching the solution for this problem for an entire day.Please explain me what is the problem and how to solve it.I want to use fontawesomeicons in my project.How to do it?
Thank you
You need to upgrade the version of fontawesomefx you are using to be compatible with your java version.
See the answer to this question, I believe your problem is the same as this person was seeing.

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

JavaFX TableView, Labels and Buttons selection is not working and looks and the table looks disabled

This is my first question in this great community. I started a Java FX tutorial on this blog http://code.makery.ch/library/javafx-8-tutorial/ . But I am using IntelliJ IDEA instead of Eclipse. The program compiles successfully but both the TableView in the left and the labels in the right look like they are disabled ( the font color is gray and not black and the table can not be highlighted on mouse hover or click ). So my question is : Why can not I select the Table view ?
TableView, Labels and Buttons looks disabled
package com.melkojji.controller;
import com.melkojji.model.Person;
import com.melkojji.view.PersonOverviewController;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private ObservableList<Person> personData = FXCollections.observableArrayList();
public Main() {
this.personData.add(new Person("Mustapha", "HUSAIN"));
this.personData.add(new Person("Mustapha", "EL KOJJI"));
}
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AdressApp");
this.primaryStage.setMinWidth(615);
this.primaryStage.setMinHeight(365);
initRootLayout();
showPersonOverview();
}
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("../view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
} catch (IOException e) {
e.printStackTrace();
}
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
}
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("../view/PersonOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(personOverview);
// Give the controller access to the main app.
PersonOverviewController controller = loader.getController();
controller.setMain(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public ObservableList<Person> getPersonData() {
return personData;
}
}
package com.melkojji.view;
import com.melkojji.controller.Main;
import com.melkojji.model.Person;
import com.melkojji.util.DateUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
/**
* Created by melkojji on 1/14/2017.
*/
public class PersonOverviewController {
#FXML
private TableView<Person> personTableView;
#FXML
private TableColumn<Person, String> personFirstNameTableColumn;
#FXML
private TableColumn<Person, String> personLastNameTableColumn;
#FXML
private Label firstNameLabel;
#FXML
private Label lastNameLabel;
#FXML
private Label streetLabel;
#FXML
private Label postalCodeLabel;
#FXML
private Label cityLabel;
#FXML
private Label birthdayLabel;
// Reference to the main application.
private Main main;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public PersonOverviewController() {
}
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
#FXML
public void initialize() {
// Initialize the person table with the two columns.
this.personFirstNameTableColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
this.personLastNameTableColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
// Clear person details.
showPersonDetails(null);
// Listen for selection changes and show the person details when changed.
personTableView.getSelectionModel().selectedItemProperty().addListener(((observable, oldValue, newValue) -> showPersonDetails(newValue)));
}
/**
* Is called by the main application to give a reference back to itself.
*
* #param main
*/
public void setMain(Main main) {
this.main = main;
// Add observable list data to the table.
this.personTableView.setItems(main.getPersonData());
}
/**
* Fills all text fields to show details about the person.
* If the specified person is null, all text fields are cleared.
*
* #param person the person or null
*/
public void showPersonDetails(Person person) {
if (person != null) {
// Fill the labels with info from the person object.
firstNameLabel.setText(person.getFirstName());
lastNameLabel.setText(person.getLastName());
streetLabel.setText(person.getStreet());
postalCodeLabel.setText(Integer.toString(person.getPostalCode()));
cityLabel.setText(person.getCity());
birthdayLabel.setText(DateUtil.format(person.getBirthday()));
// birthdayLabel.setText(...);
} else {
// Person is null, remove all the text.
firstNameLabel.setText("");
lastNameLabel.setText("");
streetLabel.setText("");
postalCodeLabel.setText("");
cityLabel.setText("");
birthdayLabel.setText("");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="325.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<top>
<MenuBar 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>
</BorderPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<AnchorPane disable="true" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.melkojji.view.PersonOverviewController">
<children>
<SplitPane dividerPositions="0.29797979797979796" prefHeight="300.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView fx:id="personTableView" editable="true" onSort="#initialize" prefHeight="298.0" prefWidth="175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="personFirstNameTableColumn" prefWidth="75.0" text="First name" />
<TableColumn fx:id="personLastNameTableColumn" prefWidth="75.0" text="Last name" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children></AnchorPane>
<AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Person details :" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0" />
<GridPane AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="30.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="First name" />
<Label text="Last name" GridPane.rowIndex="1" />
<Label text="Street" GridPane.rowIndex="2" />
<Label text="City" GridPane.rowIndex="3" />
<Label text="Postal code" GridPane.rowIndex="4" />
<Label text="Birthday" GridPane.rowIndex="5" />
<Label fx:id="firstNameLabel" text="Label" GridPane.columnIndex="1" />
<Label fx:id="lastNameLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label fx:id="streetLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label fx:id="cityLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label fx:id="postalCodeLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Label fx:id="birthdayLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" />
</children>
</GridPane>
<ButtonBar prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0">
<buttons>
<Button mnemonicParsing="false" text="New" />
<Button mnemonicParsing="false" text="Edit" />
<Button mnemonicParsing="false" text="Delete" />
</buttons>
<padding>
<Insets right="5.0" />
</padding>
</ButtonBar>
</children></AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
The disable property will disable a node and all child nodes when set. So, because you have
<AnchorPane disable="true" ..>
the anchor pane and all its subnodes, including the table, are disabled.

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.

Resources