I need to load and display some images at runtime in javafx, I managed to load one of them up, I create the ImageView passing that image as param but then the image doesn't show up. I discovered that the image is clickable (it recognizes on mouse events) but I can't display it
The controller
public class fx extends Application {
#FXML
// The reference of inputText will be injected by the FXML loader
private TextField inputUsername;
#FXML
private TextField inputUrl;
#FXML
private ComboBox inputConnectionType;
#FXML
private ComboBox inputColour;
#FXML
private Button submit;
#FXML
private ImageView imageView;
#FXML
private AnchorPane anchor;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
Image image = new Image("/AD_weapons_IT_0211.png");
imageView = new ImageView(image);
Parent root = FXMLLoader.load(getClass().getResource("/fxml/form.fxml"));
Scene scene = new Scene(root);
stage.setWidth(415);
stage.setHeight(200);
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
public void submit()
{
System.out.println(inputUsername.getText()+"\n");
System.out.println(inputColour.getValue().toString()+"\n");
System.out.println(inputConnectionType.getValue().toString()+"\n");
System.out.println(inputUrl.getText()+"\n");
}
public void press()
{
System.out.println("CLICKED");
}
}
The fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?language JavaScript?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.image.Image?>
<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="untitled" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.172-ea" fx:controller="provaProj.fx">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<ImageView fx:id = "imageView" fitHeight="150.0" fitWidth="200.0" layoutX="199.0" layoutY="112.0" onMousePressed="#press" pickOnBounds="true" preserveRatio="false" />
</children></AnchorPane>
</content>
</TitledPane>
You should have 2 Classes and 1 fxml file for an FXML-Application:
Main Class:
public class TestApplication extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLTest.fxml"));
stage.setTitle("Application");
Scene scene = new Scene(root);
stage.setWidth(415);
stage.setHeight(200);
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
your fxml-File
and a Controller-Class:
public class Controller {
#FXML
// The reference of inputText will be injected by the FXML loader
private TextField inputUsername;
#FXML
private TextField inputUrl;
#FXML
private ComboBox inputConnectionType;
#FXML
private ComboBox inputColour;
#FXML
private Button submit;
#FXML
private ImageView imageView;
#FXML
private AnchorPane anchor;
public void submit()
{
System.out.println(inputUsername.getText()+"\n");
System.out.println(inputColour.getValue().toString()+"\n");
System.out.println(inputConnectionType.getValue().toString()+"\n");
System.out.println(inputUrl.getText()+"\n");
}
#FXML
public void press()
{
System.out.println("CLICKED");
Image img = new Image("yourURL");
imageView.setImage(img);
}
}
and you can then change your image in the Controller-Class..something like I made in the press method.
Related
I've got a RootStage with some MenuItems and want to set the center of my Rootstage to an AnchorPane with a LineChart in the middle.
The chart got a CategoryAxis on the x-Axis and a NumberAxis on the y-Axis.
But now if I call the Controller for the Scene with the LineChart on it, I can see the LineChart but it has no values. Or at least doesn't show them.
Why is the LineChart not getting filled?
Or why doesn't it show the values?
MainController.class
public class MainController extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
#FXML
private MenuBar menuBar;
#FXML
private Menu menuAnsicht, menuOptionen;
#FXML
private MenuItem menuItemTag, menuItemWoche, menuItemAbout, menuItemCloseWindow;
#Override
public void init() {
}
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
initRootLayout();
}
private void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/de/mgo/temperaturstatistics/view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
logger.debug("ERROR: " + e.getMessage());
e.printStackTrace();
}
}
#FXML
public void handleTagItem(ActionEvent event) {
try {
BorderPane nowLayout = (BorderPane) menuBar.getScene().getRoot();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/de/mgo/temperaturstatistics/view/DailyScene.fxml"));
nowLayout.setCenter(loader.load());
} catch (Exception e) {
logger.debug("ERROR: " + e.getMessage());
e.printStackTrace();
}
}
DailySceneController.class
public class DailySceneController extends Application {
#FXML
private Label titleDaily;
#FXML
private LineChart<String, Number> dailyChart;
#FXML
private CategoryAxis xDaily;
#FXML
private NumberAxis yDaily;
public int count = 0;
public DailySceneController() {
count++;
System.out.println(count);
}
#Override
public void init() {
this.setChartDaily();
}
#Override
public void start(Stage primaryStage) {
}
public void setChartDaily() {
LocalDate localDate = LocalDate.now().minusDays(0);
List<Temperaturen> listeNodemcu_JHL = MainController.getListeNodemcu_JHL();
this.xDaily.setAutoRanging(false);
this.yDaily.setAutoRanging(false);
this.dailyChart.setAnimated(false);
XYChart.Series<String, Number> seriesJHL = new XYChart.Series<String, Number>();
seriesJHL.setName("Nodemcu_JHL");
seriesJHL.getData().add(new XYChart.Data<String, Number>("Test", 15));
this.dailyChart.getData().add(seriesJHL);
}
}
DailyScene.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?package de.mgo.temperaturstatistics.controller.Controller?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>
<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.mgo.temperaturstatistics.view.DailySceneController">
<top>
<Label fx:id="titleDaily" text="Temperatur-Statistik Tagesansicht" BorderPane.alignment="CENTER">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Label>
</top>
<center>
<LineChart fx:id="dailyChart" prefHeight="300.0" prefWidth="500.0" BorderPane.alignment="CENTER">
<xAxis>
<CategoryAxis fx:id="xDaily" animated="false" label="Uhrzeit" side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis fx:id="yDaily" animated="false" autoRanging="false" label="Temperatur" lowerBound="10.0" side="LEFT" tickLabelGap="1.0" tickUnit="1.0" upperBound="30.0" />
</yAxis>
</LineChart>
</center>
</BorderPane>
You're extending Application with your controller class an expect it's lifecycle methods to be invoked for it. This is bad practice and also does not result in the invocation of the lifecycle methods.
If you want a method to be invoked, use Initializable.initialize or simply a parameterless initialize method:
#FXML
private void initialize() {
this.setChartDaily();
}
There is my main class:
public class Main extends Application {
private static Stage primaryStage;
public static BorderPane mainLayout;
#Override
public void start(Stage primaryStage) {
this.setPrimaryStage(primaryStage);
primaryStage.setTitle("Project");
try {
mainLayout =
FXMLLoader.load(Main.class.getResource("/main/view/MainPage.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(mainLayout);
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
System.exit(0);
}
});
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static Stage getPrimaryStage() {
return primaryStage;
}
public void setPrimaryStage(Stage primaryStage) {
Main.primaryStage = primaryStage;
}
}
This FXML of my window:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane prefHeight="410.0" prefWidth="512.0"
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="main.controller.MainController">
<center>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0"
spacing="20.0" BorderPane.alignment="CENTER">
<children>
<Label fx:id="aaa" prefHeight="72.0" prefWidth="336.0"
text="Project" textAlignment="CENTER">
<font>
<Font name="Century Gothic" size="25.0" />
</font>
</Label>
</children>
<padding>
<Insets bottom="30.0" />
</padding>
</VBox>
</center>
</BorderPane>
This is Controller for this FXML:
public class MainController {
#FXML
private static Label aaa;
#FXML
public static void initialize(){
aaa.setText("AHOJ");
}
}
I want to call method initialize() from another class ten times like this:
public class MyClass {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
MainController.initialize();
}
}
}
But there is NullPointerException. Can someone help me?
Just remove statics for field and method, then run your application by main() in Main class:
public class MainController {
#FXML
private Label aaa;
#FXML
public void initialize(){
aaa.setText("AHOJ");
}
}
I want a Rectangle and a Label to align in the StackPane, however my code doesn't achieve the desired result:
FXML:
<fx:root type="HBox" xmlns:fx="http://javafx.com/fxml">
<StackPane fx:id="pane">
<children>
<Rectangle fx:id="bubble" fill="#ffffff"></Rectangle>
<Label fx:id="message" style="-fx-border-color:black; -fx-border-width: 1; -fx-border-style: solid;"></Label>
</children>
</StackPane>
</fx:root>
Controller:
public class MessageBubble extends HBox implements Initializable {
#FXML
private StackPane pane;
#FXML
private Label message;
#FXML
private Rectangle bubble;
#Override
public void initialize(URL location, ResourceBundle resources) {
message.setText("message");
pane.setAlignment(Pos.CENTER);
bubble.setArcWidth(15.0);
bubble.setArcHeight(15.0);
bubble.setStroke(Color.BLACK);
message.widthProperty().add(10.0).addListener((observable, oldValue, newValue) -> {
bubble.setWidth(newValue.doubleValue());
pane.layout();
});
message.heightProperty().add(10.0).addListener((observable, oldValue, newValue) -> {
bubble.setHeight(newValue.doubleValue());
pane.layout();
});
}
public MessageBubble() {
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("MessageBubble.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Update
If I do sizing programmatically, it aligns fine:
this.pane.getChildren().addAll(this.bubble, this.message);
bubble.setFill(Color.ALICEBLUE);
bubble.setArcWidth(15.0);
bubble.setArcHeight(15.0);
bubble.setStroke(Color.BLACK);
message.setStyle("-fx-border-color:black; -fx-border-width: 1; -fx-border-style: solid;");
message.setText("message");
message.setPrefWidth(60.0);
message.setPrefHeight(25.0);
bubble.setWidth(70.0);
bubble.setHeight(30.0);
But this way, I need to calculate the size myself.
Basically, if you need a Text or Label with colored backround, the easiest approach to use CSS for a single Label or Text, without any Rectangle objects.
Example to use Label with CSS
Main.java
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
HBox root = new HBox();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Label message = new Label();
TextArea textArea = new TextArea();
textArea.setPrefWidth(200);
textArea.setText("message");
message.textProperty().bind(textArea.textProperty());
message.getStyleClass().add("rounded-background-label");
root.getChildren().addAll(message, textArea);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
application.css
.rounded-background-label {
-fx-background-color: black, white;
-fx-background-insets: 0, 1;
-fx-padding: 10px;
-fx-background-radius: 12px;
}
The answer from #jewelsea on this question is really detailed:
JavaFX 2: resizable rectangle containing text
But if you want to stay with the Label+Rectangle solution
I fear it is related to this bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8137252
As workaround you can request the layout update of the StackPane, as you have tried in the listeners.
The only problem with your code is that you should replace:
pane.layout();
with
Platform.runLater(new Runnable() {
#Override
public void run() {
pane.requestLayout();
}
});
which will ensure that the layout will happen on the GUI Thread.
Example
LabelRectangleTest.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.*?>
<HBox prefHeight="100.0" prefWidth="200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.LabelRectangleTestController">
<children>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<StackPane fx:id="pane" minHeight="126.0" prefHeight="126.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<Rectangle fx:id="bubble" arcHeight="15.0" arcWidth="15.0" fill="WHITE" height="27.75" stroke="BLACK" strokeType="INSIDE" width="68.0" />
<Label fx:id="message" text="Label" />
</children>
</StackPane>
<TextArea fx:id="textArea" prefWidth="200.0" wrapText="true" />
</children>
</HBox>
</children>
</HBox>
LabelRectangleTestController.java
public class LabelRectangleTestController implements Initializable {
#FXML
private Label message;
#FXML
private Rectangle bubble;
#FXML
private StackPane pane;
#FXML
private TextArea textArea;
#Override
public void initialize(URL location, ResourceBundle resources) {
textArea.setText("message");
message.textProperty().bind(textArea.textProperty());
message.widthProperty().addListener((observable, oldValue, newValue) -> {
bubble.setWidth(newValue.intValue() + 10);
Platform.runLater(new Runnable() {
#Override
public void run() {
pane.requestLayout();
}
});
});
message.heightProperty().addListener((observable, oldValue, newValue) -> {
bubble.setHeight(newValue.doubleValue() + 10);
Platform.runLater(new Runnable() {
#Override
public void run() {
pane.requestLayout();
}
});
});
}
}
Main.java
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("LabelRectangleTest.fxml"));
HBox root = loader.load();
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I'm trying to resize a canvas in Javafx. I am using scene builder and fxml. So far, when the user clicks on the canvas the canvas turns black, and when I resize the screen and click on the canvas only the original size of the canvas turns black (canvas is not being resized). I'm not sure how to solve this. Any ideas or solutions would help alot.
Code:
Controller:
public class MainFXMLController implements Initializable
{
#FXML
private Canvas mainCanvas;
#FXML
public GraphicsContext gc;
public void initGraphics()
{
gc = mainCanvas.getGraphicsContext2D();
}
public void drawClicked(MouseEvent me)
{
gc.clearRect(0, 0, mainCanvas.getWidth(), mainCanvas.getHeight());
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, mainCanvas.getWidth(), mainCanvas.getHeight());
System.out.println("Current mosue position: " + me.getX() + ":" + me.getY());
}
#Override
public void initialize(URL url, ResourceBundle rb)
{
initGraphics();
}
}
Fxml:
<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.MainFXMLController">
<children>
<Canvas fx:id="mainCanvas" height="565.0" onMouseClicked="#drawClicked" width="750.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="35.0" />
Main Java file:
public class DrawFx extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("MainFXML.fxml"));
Scene scene = new Scene(root);
stage.setTitle("DrawFx");
stage.getIcons().add(new Image("/icon/icon.png"));
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
First some Javadocs :)
A Canvas node is constructed with a width and height that specifies the size of the image into which the canvas drawing commands are rendered. All drawing operations are clipped to the bounds of that image.
So every time the user resize the window we need to change the width of the canvas and then we need to re-draw the canvas.
Lets start by adding a fx:id to the root layout.
<AnchorPane fx:id="anchorPane" prefHeight="600.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.MainFXMLController">
<children>
<Canvas fx:id="mainCanvas" height="565.0" onMouseClicked="#drawClicked" width="750.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="35.0"/>
</children>
</AnchorPane>
Next step is to add a change listener to the root layout which will set the new height and width to the canvas and then redraw it. We can do it inside the initialize() of the controller.
public class Controller implements Initializable {
#FXML
AnchorPane anchorPane;
#FXML
private Canvas mainCanvas;
#FXML
public GraphicsContext gc;
public void initGraphics() {
gc = mainCanvas.getGraphicsContext2D();
}
public void drawClicked() {
gc.clearRect(0, 0, mainCanvas.getWidth(), mainCanvas.getHeight());
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, mainCanvas.getWidth(), mainCanvas.getHeight());
}
#Override
public void initialize(URL url, ResourceBundle rb) {
initGraphics();
anchorPane.prefWidthProperty().addListener((ov, oldValue, newValue) -> {
mainCanvas.setWidth(newValue.doubleValue());
drawClicked();
});
anchorPane.prefHeightProperty().addListener((ov, oldValue, newValue) -> {
mainCanvas.setHeight(newValue.doubleValue());
drawClicked();
});
}
}
I haven't created a new method for reDraw() since your drawClicked() wasn't doing anything. But, you can separate both the methods once it makes more sense.
The last thing is to bind to root layout's prefWidthProperty() and prefHeightProperty() to the scene's width and height respectively.
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws IOException {
AnchorPane root = FXMLLoader.load(getClass().getResource("MainFXML.fxml"));
Scene scene = new Scene(root);
stage.setTitle("DrawFx");
stage.setScene(scene);
stage.show();
root.prefWidthProperty().bind(scene.widthProperty());
root.prefHeightProperty().bind(scene.heightProperty());
}
}
If you want to resize canvas in fxml and presumably redraw its contents afterwards, the absolute minimum set is something like this:
test.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="test.TestController">
<children>
<Pane fx:id="pane" VBox.vgrow="ALWAYS">
<children>
<Canvas fx:id="canvas" height="${pane.height}" width="${pane.width}"
onWidthChange="#redraw" onHeightChange="#redraw" />
</children>
</Pane>
</children>
</VBox>
TestController.java
package test;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class TestController {
#FXML
private Canvas canvas;
#FXML
private void redraw() {
double w=canvas.getWidth();
double h=canvas.getHeight();
GraphicsContext gc=canvas.getGraphicsContext2D();
gc.clearRect(0, 0, w, h);
gc.beginPath();
gc.rect(10, 10, w-20, h-20);
gc.stroke();
}
}
Wrapping (it is not part of the functionality, just provided for completeness)
Test.java
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Test extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader=new FXMLLoader(getClass().getResource("test.fxml"));
Parent root=loader.load();
primaryStage.setTitle("Test");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
the test package is there for allowing modular magic,
module-info.java
module cnvtest {
requires transitive javafx.graphics;
requires javafx.fxml;
opens test to javafx.fxml;
exports test;
}
and there are really no more files.
I've got the following classes:
Main:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
BorderPane root = FXMLLoader.load(getClass().getResource("../view/PersonOverview.fxml"));
AnchorPane view2 = FXMLLoader.load(getClass().getResource("../view/view2.fxml"));
root.setLeft(view2);
primaryStage.setScene(new Scene(root, 1000, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
TreeController:
public class TreeController implements Initializable {
//Set icon for folder
Node folderIcon = new ImageView(new Image(this.getClass().getResourceAsStream("../icon/icon.jpg")));
//Set root
TreeItem<String> root;
#FXML TreeView<String> tree;
//Set other Items
private TreeItem<String> item1 = new TreeItem<String>("item1", folderIcon);
private TreeItem<String> item2 = new TreeItem<String>("item2", folderIcon);
private TreeItem<String> item3 = new TreeItem<String>("item3", folderIcon);
private TreeItem<String> item4 = new TreeItem<String>("item4", folderIcon);
private TreeItem<String> item5 = new TreeItem<String>("item5", folderIcon);
//Add Children to root
private void makeChildren() {
root.getChildren().add(item1);
root.getChildren().add(item2);
root.getChildren().add(item3);
root.getChildren().add(item4);
root.getChildren().add(item5);
}
#Override
public void initialize(URL location, ResourceBundle resources) {
root = new TreeItem<String>("root", folderIcon);
makeChildren();
root.setExpanded(true);
tree.setRoot(root);
}
}
And of course my view2 fxml file:
<AnchorPane
maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="354.0"
xmlns="http://javafx.com/javafx/8.0.40"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="control.TreeController">
<children>
<TreeView
layoutX="69.0" layoutY="118.0"
prefHeight="400.0" prefWidth="354.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
Now the problem I have is that it will throw a Nullpointer Exception at tree.setRoot(root);
And a ConstructLoad Exception in my Main at:
AnchorPane view2 = FXMLLoader.load(getClass().getResource("../view/view2.fxml"));
I'm still learning this stuff but I was told that when using FXML, you don't need to initialize TreeViews using "new" as the #FXML annotation will already take care of this with tree.setRoot(root).
Sorry for such a noobish question but I've been googling for the past 2 hours and haven't gotten any wiser.
I'm still learning this stuff but I was told that when using FXML, you don't need to initialize TreeViews using "new" as the #FXML annotation will already take care of this with tree.setRoot(root).
You guessed right, but in order for JavaFX to inject your Treeview (= make the "new" for you) you need to declare something like:
<Treeview fx:id="tree" />
in view2.fxml.
With the fx:id attribute setted to the same name as your Treeview variable in Java code.