JavaFX Canvas Returning Null - java

For anyone experienced with JavaFX, I have made a canvas inside my scene builder, but when I actually try to access said canvas it is null, I am not sure why as I know its made inside my scene builder and shows up in my FXML File
Ive gone ahead and pushed the code to github, it can be found here if anyone wants to look and see if I did something wrong:
https://github.com/ProSavage/JavaFXCalculator/tree/master/src/application/grapher
Feel free to give any other advice as well!
Here is the relevant code
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.grapher.GrapherController">
<children>
<HBox alignment="CENTER" layoutX="4.0" layoutY="280.0" prefHeight="20.0"
prefWidth="593.0">
<children>
<TextArea prefHeight="37.0" prefWidth="569.0" />
</children>
</HBox>
<Button layoutX="238.0" layoutY="336.0" mnemonicParsing="false"
prefHeight="50.0" prefWidth="125.0" text="Graph" />
<HBox fx:id="canvasBox" layoutX="14.0" layoutY="20.0"
prefHeight="250.0" prefWidth="570.0" />
</children>
</AnchorPane>
The class
public class GrapherController {
#FXML
private HBox canvasBox;
public void test() {
Canvas canvas = new Canvas(570,250);
canvasBox.getChildren().add(canvas);
double startX = canvas.getWidth()/2 * -1;
double endX = startX * -1;
double startY = canvas.getHeight()/2 * -1;
double endY = startY * -1;
GraphicsContext grapher = canvas.getGraphicsContext2D();
grapher.beginPath();
String equation = "x+1";
grapher.moveTo(startX,Evaluator.eval(equation.replace("x",startX + "")));
for (double i = startX; i < endX; i++) {
grapher.lineTo(startX,Evaluator.eval(equation.replace("x",String.valueOf(i))));
}
}
}
Basically when running the test method, If I create a canvas and add it to the box, the canvas is null and if I try making it into the xml file itself its still null. I feel like theres some essential step I am missing to the canvas it self.

It is actually your canvasBox field that is null. But the issue lies in your GrapherScene class. You are manually creating a GrapherController and calling test() on that instance. There is no possible way for canvasBox to not be null in that instance:
You never set it yourself
The instance wasn't created by a FXMLLoader which means no dependency injection of the FXML fields occur
Change:
public GrapherScene() {
try {
Stage grapherStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("grapher.fxml"));
Scene scene = new Scene(root, 600, 400);
//scene.getStylesheets().add(getClass().getResource("app.css").toExternalForm());
grapherStage.setScene(scene);
grapherStage.setTitle("Grapher");
grapherStage.show();
} catch (Exception ex) {
ex.printStackTrace();
}
GrapherController grapher = new GrapherController();
grapher.test();
}
To:
public GrapherScene() {
try {
Stage grapherStage = new Stage();
FXMLLoader loader = new FXMLoader(getClass().getResource("grapher.fxml"));
grapherStage.setScene(new Scene(loader.load());
grapherStage.setTitle("Grapher");
grapherStage.show();
// Must be called AFTER loader.load()
// Method has generic return type so explicit casting is not necessary in this case
GrapherController grapher = loader.getController();
grapher.test();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Note: I'm using the instance FXMLLoader.load() method here and not the static FXMLLoader.load(URL) method.
For more information:
Accessing FXML controller class
JavaFX - How to get FXML Controller? [duplicate]
Passing Parameters JavaFX FXML
Introduction to FXML

Related

How do i start a JavaFX GUI in an own thread

I have a Project, where I need to start a GUI but in another thread that is not my main thread.
-> The first main thread starts...
-> If it decides to show a GUI it starts my GUI.
-> All other calculations should still happen in the main thread.
What I mean with this is, that i can't, ounder no circumstance, start the gui in the main thread. And i need to be able to comunicate with my Controller (no controller in the sample). But when i do the normal: .. Start extends Application { .. approach, I cant communicate with the controller anymore because the thread is occupied. The code below should allow me to do everything I need to do, so I hope there is a way I can make it work.
The problem, why my code doesn't work is a Exception:
Exception in thread "Thread-0" java.lang.IllegalStateException: Toolkit not initialized
When calling Platform.runLater() .. in my GuiThread class.
Thanks four your help in advance.
My main class:
public class Start{
private void start() {
GuiThread thread = new GuiThread();
thread.start();
System.out.println("here continues the thread, while the GUI is shown");
}
public static void main(String[] args) {
Start main = new Start();
main.start();
}
}
My custom thread class, where my GUI should be started:
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class GuiThread extends Thread {
#Override
public void run() {
super.run();
// Here i have objects i need to have in the controller
// Then i have to start my GUI
// When calling Platform.runLater()... this error shows : Exception in thread "Thread-0" java.lang.IllegalStateException: Toolkit not initialized
Platform.runLater(() -> {
Group root;
try {
Stage stage = new Stage();
root = new Group();
Scene scene = new Scene(root);
FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
Node node = loader.load();
// Controller stuff
root.getChildren().add(node);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
Just a sample FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<Label text="left" />
</children>
</StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<Label text="right" />
</children>
</StackPane>
</children>
</HBox>
Usually you'd start any additional threads from the Application.init or Application.start methods, but in your case this doesn't seem to be an option.
Starting with JavaFX 9 you could use Platform.startup the first time you need access to JavaFX. After the Runnable passed to that method is executed you should be able to use Platfrom.runLater as you're used to.
Using this approach you need to make sure to shut when all other logic has completed and you're sure you don't need to display any GUI.
Platform.startup(() -> {
Group root;
try {
Stage stage = new Stage();
root = new Group();
Scene scene = new Scene(root);
FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
Node node = loader.load();
// Controller stuff
root.getChildren().add(node);
stage.setScene(scene);
stage.setOnHidden(evt -> Platform.exit()); // make sure to completely shut down JavaFX when closing the window
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
});

Add a custom component in JavaFX

I would like to add a custom element into a VBox.
For example: being able to write VBox.getChildren().add(element) and element is a custom node created by me in FXML.
I already followed this tutorial: https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm
but the example only shows how to do this inside the same controller (a single controller, i already have my "big" controller, which is WinnerController, I would like to split the two classes, one that manages the single element, and one that manages the whole scene Winner.fxml).
I already have a class WinnerController which is the controller of my FXML Winner.fxml.
Here the code of my Winner.fxml:
<AnchorPane id="paneWinner" fx:id="paneWinner" prefHeight="800.0" prefWidth="1280.0" stylesheets="#winner.css" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.gui.WinnerController">
<children>
<VBox layoutX="434.0" layoutY="125.0" prefHeight="250.0" prefWidth="413.0" spacing="10.0">
<children>
<AnchorPane id="leaderBoardElement" prefHeight="80.0" prefWidth="413.0" stylesheets="#leaderBoard.css">
<children>
<Text layoutX="49.0" layoutY="45.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text" wrappingWidth="107.13671875" />
</children></AnchorPane>
</children>
</VBox>
I would like to dynamically add element with the id "leaderBoardElement" (so an AnchorPane + Text) into my VBox.
How can i do that?
Edit: I also tried with this solution:How to understand and use `<fx:root>` , in JavaFX?
but i keep getting nothing. When i do vbox.getChildren().add(new MyComponent());
called in my WinnerControlleri get nothing.
WinnerController class:
public class WinnerController implements Initializable {
#FXML
private VBox leaderBoard;
#FXML
public void initialize(URL location, ResourceBundle resources) {
System.out.println("Winner Init");
MyComponent test = new MyComponent();
System.out.println(test);
// leaderBoard.getChildren().add(new MyComponent());
}
}
My Component class:
public class MyComponent extends AnchorPane {
#FXML
private TextField textField ;
#FXML
private Button button ;
public MyComponent() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("MyComponent.fxml"));
loader.setController(this);
loader.setRoot(this);
loader.load();
textField.setText("HELLO!");
} catch (IOException exc) {
// handle exception
System.out.println("ELEMENT NOT CREATE!!!");
}
}
}
Winner.fxml:
<AnchorPane id="paneWinner" fx:id="paneWinner" prefHeight="800.0"
prefWidth="1280.0" stylesheets="#winner.css" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.gui.WinnerController">
<children>
<VBox fx:id="leaderBoard" layoutX="434.0" layoutY="125.0" prefHeight="250.0" prefWidth="413.0" spacing="10.0" />
MyComponent.fxml:
<fx:root type="javafx.scene.layout.AnchorPane" fx:id="leaderBoardElement" id="leaderBoardElement">
<TextField fx:id="textField" />
<Button fx:id="button" />
And i call the creation and loading of Winner.fxml from another class like this:
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Winner.fxml"));
if (loader!=null)
System.out.println("LOADER NOT NULL!!");
try{
System.out.println("TRY!");
Parent root = (Parent) loader.load();
if(root!=null)
System.out.println("ROOT NOT NULL!!");
Scene startedGame = new Scene(root, 1280, 800, Color.WHITE);
if(startedGame!=null)
System.out.println("SCENE NOT NULL!");
Stage window = (Stage) paneCarta0.getScene().getWindow();
if (window!=null)
System.out.println("WINDOW NOT NULL!!");
window.setScene(startedGame);
window.show();
catch (IOException Exception) {
System.out.println("View not found. Error while loading");
}
The problem is inside the new MyComponent(), where probably i get an exception and it propagates to my main caller. I've tried everything but i can't figure out why it can't create the MyComponent object.
If your custom component is in a separate FXML file you can do this.
In your WinnerController class:
#FXML
private void initialize() throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getRessource("customElement.fxml"));
fxmlLoader.setController(new CustomElementController()); //Or just specify the Controller in the FXML file
myVBox.getChildren().add(fxmlLoader.load());
}
Edit: In this solution there should be NO <fx:include> tags in your main fxml

How to obtain data from a database and put it inside fxml?

I'm making some kind of information application about a city which I need to use a database for, I got my database set up and ready and most of the code needed as well; the only part is to extract the data I need from my SQL server into a BarChart in fxml. Can anyone give me a example or something?
My fxml:
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.ControllerZW">
<BarChart fx:id="graph" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</BarChart>
</Pane>
My controller atm:
package Controllers;
public class ControllerZW implements Interface.Ibacktomenu {
#Override
public void backtomenu() {
try {
Main.mainStage.setScene(
new Scene(FXMLLoader.load(getClass().getResource("../scenes/MainMenu.fxml")))
);
} catch (IOException e) {
System.out.println("Probably couldnt find resource file");
e.printStackTrace();
}
}
}
Do you want to render the data form database to XML format?
I think some template engine maybe help you, such as Freemarker.
You could use Freemarker as view of Spring MVC, please refer to http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/
Use a Task to get the data from the database and any method described in Passing Parameters JavaFX FXML to get the data to the controller, e.g.
#Override
public void start(Stage primaryStage) {
Task<Pane> dataLoader = new Task<Pane>() {
#Override
protected Pane call() throws Exception {
// create data
XYChart.Series<String, Double> rain = new XYChart.Series<>();
rain.setName("rain");
Month[] months = Month.values();
for (Month month : months) {
// simulates slow database connection; just adding some random values here
Thread.sleep(200);
rain.getData().add(new XYChart.Data<>(month.toString(), Math.random() * 100));
}
// load chart (replace BarChartLoad.class.getResource("chart.fxml") with your own URL)
FXMLLoader loader = new FXMLLoader(BarChartLoad.class.getResource("chart.fxml"));
Pane pane = loader.load();
// pass data to controller
loader.<ControllerZW>getController().setData(FXCollections.observableArrayList(rain));
return pane;
}
};
// placeholder
Pane root = new Pane();
Scene scene = new Scene(root, 1000, 600);
// set new scene root on successfull completion of the task
dataLoader.setOnSucceeded(evt -> scene.setRoot(dataLoader.getValue()));
// TODO: handle task failure
new Thread(dataLoader).start();
primaryStage.setScene(scene);
primaryStage.show();
}
public class ControllerZW implements Interface.Ibacktomenu {
#FXML
private BarChart<String, Double> graph;
public void setData(ObservableList<XYChart.Series<String, Double>> data) {
graph.setData(data);
}
...

why Javafx standalone application accepting double click?

Here is my Main java file.The probelm is that the Whole project is working on double click when i launch it.
public Stage primaryStage;
public BorderPane rootLayout;
private Pane splashLayout;
private ProgressBar loadProgress;
private Label progressText;
private static final int SPLASH_WIDTH = 600;
private static final int SPLASH_HEIGHT = 400;
public static void main(String[] args) throws Exception {
launch(args);
}
#Override
public void start(final Stage initStage) throws Exception {
//this.primaryStage = primaryStage;
FXMLLoader fxmlLoader1 = new FXMLLoader(getClass().getResource(
"KDAF_Splash.fxml"));
try {
// flag = false;
Parent root2 = fxmlLoader1.load();
Stage stage2 = new Stage();
stage2.initStyle(StageStyle.TRANSPARENT);
// masterpane = new Pane();
// masterpane.setOpacity(1);
// stage2.setTitle("Create New Project");
stage2.setScene(new Scene(root2, 600, 400));
stage2.show();
PauseTransition delay = new PauseTransition(Duration.seconds(5));
delay.setOnFinished( event ->{ stage2.close();showMainStage();} );
delay.play();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
};
private void showMainStage(
) {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Preloader.class.getResource("RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Stage stage=new Stage();
stage.setTitle("Activation");
Scene scene = new Scene(rootLayout);
//stage.initStyle(StageStyle.DECORATED);
stage.setScene(scene);
stage.initStyle(StageStyle.UNDECORATED);
stage.show();
showPersonOverview();
//mainStage.show();
/* mainStage.setTitle("Activation Wizard");
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
// primaryStage.initStyle(StageStyle.UTILITY);
mainStage.setResizable(false);
mainStage.initStyle(StageStyle.UNDECORATED);
mainStage.setScene(scene);
mainStage.show();
showPersonOverview();*/
} catch (IOException e) {
e.printStackTrace();
}
}
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("activation wizard.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
personOverview.setPrefHeight(400);
// Set person overview into the center of root layout.
rootLayout.setCenter(personOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
private void showSplash(
final Stage initStage,
Task<?> task,
InitCompletionHandler initCompletionHandler
) {
progressText.textProperty().bind(task.messageProperty());
loadProgress.progressProperty().bind(task.progressProperty());
task.stateProperty().addListener((observableValue, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED) {
loadProgress.progressProperty().unbind();
loadProgress.setProgress(1);
initStage.toFront();
FadeTransition fadeSplash = new FadeTransition(Duration.seconds(1.2), splashLayout);
fadeSplash.setFromValue(1.0);
fadeSplash.setToValue(0.0);
fadeSplash.setOnFinished(actionEvent -> initStage.hide());
fadeSplash.play();
initCompletionHandler.complete();
} // todo add code to gracefully handle other task states.
});
Scene splashScene = new Scene(splashLayout);
initStage.initStyle(StageStyle.UNDECORATED);
final Rectangle2D bounds = Screen.getPrimary().getBounds();
initStage.setScene(splashScene);
initStage.setX(bounds.getMinX() + bounds.getWidth() / 2 - SPLASH_WIDTH / 2);
initStage.setY(bounds.getMinY() + bounds.getHeight() / 2 - SPLASH_HEIGHT / 2);
initStage.show();
}
public interface InitCompletionHandler {
public void complete();
}
}
The FXml is given below. Please help me to resolve this issue. If the question is not clear help me to explain better. The main problem is when the application is launched after splash screen. The button accepts double click and then moves to load next FXML. this is been very painful as i have to double click everytime instead of single click. I am not able to identify the problem here. Whether it is a problem that persists from scene builder or the problem is in the main preloader class.Even the menu item is accepting double click. I try to google the solution for but could not find any solution. please look int the code and help to resolve this issue. I have done a lot of R&D but all went vain . There might be a little problem in it but i am not being able to identify it. Please ignore the formatting of code and unused variables. The code is not final yet. finally i would like to remind the main problem is that after launching, the application is working only on double click. lets look at the work flow to understand it better. SPLASH SCREEN->ACTIVATION WIZARD-DOUBLE CLICK ON A BUTTON->TIP SCREEN-DOUBLE CLICK ON A BUTTON->PROJECT SCREEN->DOUBLE CLICK TO OPEN ANYTHING. It is really frustrating. please help.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-border-color: black;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="EventHandlingController">
<children>
<Button layoutX="301.0" layoutY="333.0" mnemonicParsing="false" text="Buy Full Version" />
<Button fx:id="myButton7" layoutX="420.0" layoutY="333.0" mnemonicParsing="false" onAction="#Showtip" prefHeight="25.0" prefWidth="114.0" text="Continue Trial " />
<TextField editable="false" layoutX="85.0" layoutY="108.0" prefHeight="151.0" prefWidth="449.0" style="-fx-background-color: silver;" text="You have <xx> days left on your trial.">
<font>
<Font size="21.0" />
</font>
</TextField>
<Label layoutX="85.0" layoutY="23.0" prefHeight="37.0" prefWidth="135.0" text="Activation Wizard">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<Button fx:id="closebutt" layoutX="573.0" layoutY="2.0" mnemonicParsing="false" onAction="#closebuttonD" prefHeight="15.0" prefWidth="20.0" text="X" />
</children>
</AnchorPane>

Different Anchor Results in Scene Builder and Application

I generated the following FXML in Scene Builder. When I preview it in Scene Builder and resize the window the inner split view maintains the spacing between itself and the anchor view. However when I run the FXML in my application it does not. Any ideas? I am using javafx 2.2.51.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="477.0" prefWidth="515.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.nm.haze.LibraryViewController">
<children>
<SplitPane dividerPositions="0.09484536082474226" focusTraversable="true" prefHeight="415.0" prefWidth="487.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="48.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<ListView prefHeight="371.0" prefWidth="425.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
And my controller code:
package com.nm.haze;
import java.net.URL;
import java.util.ResourceBundle;
public class LibraryViewController extends BaseController {
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}
Main code
private static final String LIBRARY_SELECT = "LibrarySelect";
private static final String LIBRARY_VIEW = "LibraryView";
#Override
public void start(Stage primaryStage) throws IOException {
SceneController sceneController = new SceneController(primaryStage, new SettingsManager());
sceneController.loadScene(LIBRARY_SELECT);
sceneController.loadScene(LIBRARY_VIEW);
if (sceneController.setupNeeded()) {
sceneController.setScreen(LIBRARY_SELECT);
} else {
sceneController.setScreen(LIBRARY_VIEW);
}
Group root = new Group();
root.getChildren().addAll(sceneController);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
SceneController code:
public class SceneController extends StackPane {
Map<String, Node> scenes = new HashMap<>();
Stage stage;
SettingsManager settingsManager;
public SceneController(Stage stage, SettingsManager settingsManager) {
this.stage = stage;
this.settingsManager = settingsManager;
}
public void addScene(String name, Node scene) {
scenes.put(name, scene);
}
public void loadScene(String name) throws IOException {
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(name + ".fxml"));
Parent loadScreen = (Parent)myLoader.load();
((BaseController)myLoader.getController()).setSceneController(this);
addScene(name, loadScreen);
}
public void setScreen(final String name) {
List<Node> children = getChildren();
Node scene = scenes.get(name);
if(scene != null) {
if (children.size() > 0) {
children.remove(0);
}
children.add(0, scene);
} else {
throw new IllegalArgumentException("Scene has not been loaded");
}
}
public Stage getStage() {
return stage;
}
public boolean setupNeeded() {
return settingsManager.setupNeeded();
}
}
Just to be clear about what the issue is, please see the before and after screenshots below. The distance between the ListView and the AnchorPane should stay the same (and does in Scene Builder).
`You just have to fix the minimum and maximum width of the part of the SplitPane, whose width you dont want to change on resizing your window !
Here I have fixed the left part, by setting the minWidth and maxWidth of AnchorPane on the left side. You can do the same for the right side as well, depending on your requirement.
abcFxml.fxml
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="477.0" prefWidth="515.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="bounty.LibraryViewController">
<children>
<SplitPane dividerPositions="0.09484536082474226" focusTraversable="true" prefHeight="415.0" prefWidth="487.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="48.0">
<items>
<AnchorPane minHeight="0.0" minWidth="100.0" prefHeight="160.0" prefWidth="100.0" maxHeight="160.0" maxWidth="100.0" />
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<ListView prefHeight="371.0" prefWidth="425.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
Controller
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class LibraryViewController extends Application
{
#Override
public void start(Stage arg0) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("abcFxml.fxml"));
AnchorPane pane = (AnchorPane) fxmlLoader.load();
Scene scene = new Scene(pane);
arg0.setScene(scene);
arg0.show();
}
public static void main(String args[])
{
launch(args);
}
}
Screenshots
Size1
Size 2
EDIT : As per user updation
The problem you are facing is because of the Group you are using. Group itself is not resizable ! Try to put the StackPane directly to your scene(like I did) or you can use any other container like VBOX / HBOX etc
#Override
public void start(Stage primaryStage) throws IOException {
SceneController sceneController = new SceneController(primaryStage, new SettingsManager());
sceneController.loadScene(LIBRARY_SELECT);
sceneController.loadScene(LIBRARY_VIEW);
if (sceneController.setupNeeded()) {
sceneController.setScreen(LIBRARY_SELECT);
} else {
sceneController.setScreen(LIBRARY_VIEW);
}
primaryStage.setScene(new Scene(sceneController));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}

Categories