JavaFX Switch Scene in a SplitPane? - java

At the Base I have an AnchorPane then a SplitPane. On the left pane I have a listView and depending on the list element selected, the right pane displays the appropriate content. The way I have done this is by overlapping AnchorPanes and setting them to .setVisible(false) initially and as they are selected I set them to .setVisible(true) like so :
public void listSelection() {
String selection = listView.getSelectionModel().getSelectedItem();
switch(selection) {
case "Speed of sound":
disableOld(); // disables old AnchorePane
response.setText("Speed of sound conversion");
AnchorPane1.setVisible(true);
break;
case "Temperature conversion":
disableOld();
response.setText("Temperature conversion");
AnchorPane2.setVisible(true);
break;
}
}
I would like to know how to produce the same effect visually but with different scenes as I would like for each new AnchorPane to have it's own FXML and ControllerClass.

You can implement something like this :
Your main class :
public void start(Stage primaryStage) throws IOException {
primaryStage.setTitle("Title");
primaryStage.setScene(createScene(loadMainPane("path_of_your_fxml")));
primaryStage.show();
}
private Pane loadMainPane(String path) throws IOException {
FXMLLoader loader = new FXMLLoader();
Pane mainPane = (Pane) loader.load(
getClass().getResourceAsStream(path));
return mainPane;
}
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(mainPane);
return scene;
}
public static void main(String[] args) {launch(args); }
Then you can create a separate class call Navigator to store all your fxml paths:
public class Navigator {
private final String P1;
private final String P2;
//then you can implement getters...
public String getP1() {
return P1;
}
public String getP2() {
return p2;
}
private static FxmlController Controller;
public static void loadPane(String fxml) {
try {
FxmlController.setPane(
(Node) FXMLLoader.load(Navigator.class.getResource(fxml)));
} catch (IOException e) {
e.printStackTrace();
}
}
public Navigator() throws IOException {
this.P1 = "p1.fxml";
this.P2 = "p2.fxml";}
In your main FxmlController(which is the controller of the permanent layer of your application , rest of the stack-panes-{p1 and p2} will load on your permanent layer )
This is how you load layers on the main FxmlController :
#FXML
private StackPane stackPaneHolder;
...
public void setPane(Node node) {
if (stackPaneHolder.getChildren().isEmpty()) {
//if stackPaneHolder is empty
stackPaneHolder.getChildren().add(node);
} else {
if (stackPaneHolder.getClip() != node) {
//if stackPaneHolder is not empty then remove existing layer and add new layer
stackPaneHolder.getChildren().remove(0);
stackPaneHolder.getChildren().add(0, node);
}
}
}
Then you can load panes by pressing a button like below :
#FXML
private void btnAction(ActionEvent event) throws IOException {
Navigator.load(new Navigator().getP1());
..
This is how it works :

Related

JavaFX SetOnCloseRequest() closing all screens

In my application I have multiple .fxml files. When I set a screen in my application, I also set the SetOnCloseRequest() method. For some screens is the code in that method just to switch to another screen. For example: When I have my Create Bank screen open and I press the close button, I want to switch back to my Manage Banks screen. But when I press the close button, it seems to switch the screens correctly, but very shortly after it closes the Manage Banks screen for some reason and the application dont stops running. So I cant do anything after that, because I don't have any GUI.
Does someone knows how to make sure that the Manage Banks screens does not close?
Here is my main class with theSetOnCloseRequest() method:
public class ClientMain extends Application {
public static String screenCreateBankId = "createBank";
public static String screenCreateBankFile = "Screens/createBank.fxml";
public static String screenCreateBankAccountId = "createBankAccount";
public static String screenCreateBankAccountFile = "Screens/createBankAccount.fxml";
public static String screenLoginId = "login";
public static String screenLoginFile = "Screens/login.fxml";
public static String screenManageBanksId = "manageBanks";
public static String screenManageBanksFile = "Screens/manageBanks.fxml";
private static Stage primaryStage;
private static ScreensController mainContainer;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
ClientMain.primaryStage = primaryStage;
mainContainer = new ScreensController();
mainContainer.loadScreen(ClientMain.screenCreateBankId, ClientMain.screenCreateBankFile);
mainContainer.loadScreen(ClientMain.screenLoginId, ClientMain.screenLoginFile);
mainContainer.loadScreen(ClientMain.screenManageBanksId, ClientMain.screenManageBanksFile);
mainContainer.setScreen(ClientMain.screenLoginId);
Group root = new Group();
root.getChildren().addAll(mainContainer);
primaryStage.setResizable(false);
primaryStage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void setProperties(String name){
primaryStage.sizeToScene();
primaryStage.centerOnScreen();
switch (name){
case "createBank":
primaryStage.setTitle("Create Bank");
primaryStage.setOnCloseRequest(event -> mainContainer.setScreen(screenManageBanksId));
break;
case "login":
primaryStage.setTitle("Login");
primaryStage.setOnCloseRequest(event -> System.exit(0));
break;
case "manageBanks":
primaryStage.setTitle("Manage Banks");
primaryStage.setOnCloseRequest(event -> mainContainer.getManageBanksController().logoutAdmin());
break;
}
}
}
And here is my ScreensController class:
public class ScreensController extends StackPane {
private HashMap<String, Node> screens = new HashMap<>();
private Client client;
private NewTransactionController newTransactionController;
private BankAccountController bankAccountController;
private ManageBanksController manageBanksController;
public Client getClient() {
return this.client;
}
public NewTransactionController getNewTransactionController() {
return this.newTransactionController;
}
public BankAccountController getBankAccountController() {
return this.bankAccountController;
}
public ManageBanksController getManageBanksController() {
return this.manageBanksController;
}
public ScreensController() {
try {
this.client = new Client();
System.out.println("Client: Client created");
} catch (RemoteException e) {
System.out.println("Client: Cannot create Client");
System.out.println("Client: RemoteException: " + e.getMessage());
System.exit(0);
}
}
public void addScreen(String name, Node screen) {
screens.put(name, screen);
}
public void loadScreen(String name, String resource) {
try {
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Parent loadScreen = myLoader.load();
IControllers myScreenController = myLoader.getController();
if (myScreenController instanceof NewTransactionController) {
this.newTransactionController = (NewTransactionController) myScreenController;
} else if (myScreenController instanceof BankAccountController) {
this.bankAccountController = (BankAccountController) myScreenController;
} else if (myScreenController instanceof ManageBanksController) {
this.manageBanksController = (ManageBanksController) myScreenController;
}
myScreenController.setScreenParent(this);
addScreen(name, loadScreen);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setScreen(final String name) {
if (screens.get(name) != null) {
if (!getChildren().isEmpty()) {
getChildren().remove(0);
getChildren().add(0, screens.get(name));
ClientMain.setProperties(name);
} else {
//First time start up
getChildren().add(screens.get(name));
ClientMain.setProperties(name);
}
} else {
System.out.println("Screen hasn't been loaded!!!");
}
}
}
try this
primaryStage.setOnCloseRequest(event -> {
event.consume();
mainContainer.setScreen(screenManageBanksId);
});
...
primaryStage.setOnCloseRequest(event -> {
event.consume();
mainContainer.getManageBanksController().logoutAdmin();
});

Connecting Classes in JavaFX

I have been working on JavaFX and trying figure out how to connect classes contained withing the package. I want the "text1btn" button from MainController class to send a text from "scene1TextField" also in MainController class to TextArea in LeftTextArea class. I would appreciate any comments on that. Thank you.
package sample;
public class Main extends Application {
public static BorderPane root = new BorderPane();
public static BorderPane getRoot() {
return root;
}
#Override
public void start(Stage primaryStage) throws Exception {
URL url1 = getClass().getResource("../view/MainView.fxml");
BorderPane bp1 = FXMLLoader.load(url1);
URL url2 = getClass().getResource("../view/LeftTextArea.fxml");
AnchorPane bp2 = FXMLLoader.load(url2);
root.setTop(bp1);
root.setCenter(bp2);
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package Controller;
public class MainController {
#FXML
Button scene1btn;
#FXML
Button scene2btn;
#FXML
TextField scene1TextField;
#FXML
TextField scene2TextField;
#FXML
Button text1btn;
#FXML
Button text2btn;
#FXML
TextArea mainViewTextArea;
#FXML
public void initialize() {
}
#FXML
public void text1btnClicked() {
}
#FXML
public void text2btnClicked() {
}
#FXML
private void scene1btnClicked() {
try {
URL url1 = getClass().getResource("../view/LeftTextArea.fxml");
AnchorPane bp1 = FXMLLoader.load(url1);
BorderPane border = Main.getRoot();
border.setCenter(bp1);
} catch (IOException e) {
e.printStackTrace();
}
}
#FXML
private void scene2btnClicked() {
try {
URL url2 = getClass().getResource("../view/RightTextArea.fxml");
AnchorPane bp2 = FXMLLoader.load(url2);
BorderPane border = Main.getRoot();
border.setCenter(bp2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package Controller;
public class LeftTextArea {
#FXML
public TextArea leftTextArea;
}
A quick and simple approach is just to expose a StringProperty in the MainController, and when it changes call a method in the LeftTextArea:
public class MainController {
private final StringProperty text = new SimpleStringProperty();
public StringProperty textProperty() {
return text ;
}
// existing code ...
#FXML
public void text1btnClicked() {
textProperty().set(scene1TextField.getText());
}
// ...
}
In LeftTextArea do
public class LeftTextArea {
#FXML
public TextArea leftTextArea;
public void setText(String text) {
leftTextArea.setText(text);
}
}
And then you can tie it all together with
#Override
public void start(Stage primaryStage) throws Exception {
URL url1 = getClass().getResource("../view/MainView.fxml");
FXMLLoader loader1 = new FXMLLoader(url1);
BorderPane bp1 = loader1.load();
MainController mainController = loader1.getController();
URL url2 = getClass().getResource("../view/LeftTextArea.fxml");
FXMLLoader loader2 = new FXMLLoader(url2);
AnchorPane bp2 = loader2.load();
LeftTextArea leftTextArea = loader2.getController();
mainController.textProperty().addListener((obs, oldText, newText) ->
leftTextArea.setText(newText));
root.setTop(bp1);
root.setCenter(bp2);
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
If you end up needing multiple properties like this that are essentially shared between controllers, you probably need to define a "model" class to encapsulate them all in one place, and pass the model to the controllers. See, e.g. JavaFX controller to controller - access to UI Controls or Applying MVC With JavaFx
If you want to set any field in the class LeftTextArea just simply create a public setter method in Class LeftTextArea like
public void setTextArea(Text text){
//do what you want to do
}
Then call the method from MainController class with the object of LeftTextArea class. like
LeftTextArea leftTextArea = new LeftTextArea();
leftTextArea.setTextArea(text); //text is the desired you want to send

JavaFX TableView Issues Adding Rows + Organizing Multi Controller and FXML App

I'm new to JavaFX and I've been at this code for about 8 hours now and I've become a bit delusional with the code. My two main problems are:
Can't add new items to the TableView using my popUp box display().
Feels messy and unorganized. Any tips for better communication between FXML and Controllers? (Again I'm new so it could be that I've stared too long at it)
My main class
public class Main extends Application {
public static Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage window) throws Exception {
try {
primaryStage = new Stage();
window = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("Fissto.fxml"));
Scene scene = new Scene(root);
window.setTitle("Fissto - the File Storage App!");
window.setScene(scene);
window.show();
}catch(Exception e){
e.printStackTrace();
}
// C.setLibraryStage();
}
}
My main Controller class (I have two sub ones that connect in the Fissto.fxml)
public class Controller implements Initializable{
Main main;
#FXML LibraryController libraryController = new LibraryController();
#FXML MergePageController mergePageController = new MergePageController();
private AddImageController addImageController = new AddImageController();
#FXML public void initialize(URL location, ResourceBundle resources){
System.out.println("View is now loaded!");
main = new Main();
libraryController.init(this);
mergePageController.init(this);
addImageController.init(this);
}
//Interface Initialization
public void setMergeStage() throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Controllers/MergePage.fxml"));
Scene scene = new Scene(root);
main.primaryStage.setScene(scene);
}
public void setLibraryStage() throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Controllers/LibraryPage.fxml"));
Scene scene = new Scene(root);
main.primaryStage.setScene(scene);
}
//Closing a window
public void closeWindow(){
main.primaryStage.close();
}
}
And finally the controller for the page that holds the TableView
public class LibraryController {
private Controller main;
//Library TableView Controllers
#FXML public TableView<Image> library;
#FXML private TableColumn<Image, String> NameColumn = new TableColumn<>();
#FXML private TableColumn<Image, ArrayList<String>> TagsColumn = new TableColumn<>();
#FXML private TableColumn<Image, String> CommentsColumn = new TableColumn<>();
#FXML private TableColumn<Image, String> FileLocationColumn = new TableColumn<>();
#FXML private TableColumn<Image, Integer> PointsColumn = new TableColumn<>();
public void init(Controller main){
System.out.println("LibraryPage Loading");
this.main = main;
addDataToColumns();
library = new TableView<>();
library.getItems().setAll(getImages());
System.out.println("LibraryPage Loaded");
}
//Initializes the column titles
private void addDataToColumns(){
NameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TagsColumn.setCellValueFactory(new PropertyValueFactory<>("tags")); //TODO Convert to String format
CommentsColumn.setCellValueFactory(new PropertyValueFactory<>("comments"));
FileLocationColumn.setCellValueFactory(new PropertyValueFactory<>("filelocation"));
PointsColumn.setCellValueFactory(new PropertyValueFactory<>("points"));
}
//Gets all of the images
private ObservableList<Image> getImages() {
//TODO: Add where to actually get the data from
ObservableList<Image> images = FXCollections.observableArrayList();
String s = "Dog, Cat, Jumping Jack,";
ArrayList<String> list = Image.getTagOrganizer(',', s);
images.add(new Image("Test", list, "Comment", "No File Location, yet!", 10));
String k = "Calculus, Complex Numbers, Hard dude,";
ArrayList<String> list2 = Image.getTagOrganizer(',', k);
images.add(new Image("Number2", list2, "I love MathClub", "No File Location, yet!", -10));
return images;
}
This last class is the popup menu that takes in input to put in the GridPane
public class AddImageController {
private Controller main;
public void init(Controller main){
System.out.println("ImagePage Loaded");
this.main = main;
}
//Submitting an image to the library from the AddImagePage
public TextField nameInput;
public TextField tagsInput;
public TextField commentInput;
public TextField pointsInput;
public Label errorMessage;
/** TODO: Make it so that it writes to file then theoretically, the main controller should read from file every so often
* Main functionality for adding the information from the form to the database */
public void submitImage(){
if(!(nameInput.getText().trim().isEmpty()) && !(tagsInput.getText().trim().isEmpty()) && !(pointsInput.getText().trim().isEmpty())) {
if (isInt(pointsInput)) {
// System.out.print("Sent to database, *whoosh!*");
LibraryController c = new LibraryController();
ArrayList<String> s = Image.getTagOrganizer(',', tagsInput.getText());
Image image = new Image(nameInput.getText(), s, commentInput.getText(),"Location Needed", Integer.parseInt(pointsInput.getText()));
c.library.getItems().add(image);
clearInputs();
}
}else {
errorMessage.setText("Fill every field");
}
}
//Clears the input fields in the AddImagePage
public void clearInputs(){
nameInput.clear();
tagsInput.clear();
commentInput.clear();
pointsInput.clear();
errorMessage.setText("");
}
//Submission format verifiers
private boolean isInt(TextField input){
try{
int i = Integer.parseInt(input.getText());
errorMessage.setText("");
return true;
}catch (NumberFormatException e){
System.out.println("Oh no: " + input.getText() + " is not an integer");
errorMessage.setText("Points must be a number");
return false;
}
}
//Image Selection Handler
public void imageSelectionHandler(){
}
}
I understand it may be hard to read, so any feedback on how to make it easier to read in the future is much appreciated.

JavaFX - Giving a class access to a controller method to draw on a canvas

I have the following files in my project:
main.java for starting up the application
RootLayout.fxml & RootlayoutController.java which serves as the main stage and will be used for a menu bar
Overview.fxml and OverviewController.java for the main window of the application
point.java which contains the application logic
The OverviewController's test()-method is triggered by a Button's onAction-Event.
Essentially I am looking for a way to give the Point.java class access to the OverviewController.java class, so it can call the associated drawPoint(double x, double y) method.
I have been researching this question for quite a while now, but have been unable to find an understandable answer - since my knowledge of JavaFX is somewhat limited.
My sincere thanks for taking your time to answer my question.
Main.java
public class Main extends Application {
public Stage primaryStage;
private BorderPane rootLayout;
public Main(){
}
#Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
this.primaryStage.setTitle("");
initRootLayout();
showOverview();
}
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();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.setHeight(900);
primaryStage.setWidth(900);
// Give the controller access to the main app.
RootLayoutController controller = loader.getController();
controller.setMainApp(this);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showOverview(){
try {
// Load Overview
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/Overview.fxml"));
AnchorPane overview = (AnchorPane) loader.load();
// Set overview into the center of root layout.
rootLayout.setCenter(overview);
// Give the controller access to the main app.
OverviewController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
OverviewController.java
public class OverviewController {
private sample.Main Main;
public void setMainApp(Main mainApp) {
this.Main = mainApp;
}
#FXML
Canvas canvas;
public void test(){
Point point = new Point(5,5);
point.drawPoint();
}
public void draw(double x, double y){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.rgb(255, 0, 0));
gc.fillOval(x-4, y-4, 8, 8);
}
}
Point.java
public class Point {
public double x;
public double y;
point(double x, double y){
this.x = x;
this.y = y;
}
drawPoint(){
// This is where I want to build a reference to OverviewController.java's draw(double x, double y)-Method
}
}
I would recommend using a Model-View-Controller (or similar pattern) approach, instead of trying to let the data type Point have access to the controller class.
That way you can let any controller that has access to the model generate points. Your OverviewController just has to observe the model (or specific properties it contains) in order to draw the points onto the canvas, etc.
Create a model class:
public class Model {
private final ObservableList<Point> points = FXCollections.observableArrayList();
public ObservableList<Point> getPoints() {
return points ;
}
// other data you need to share...
}
Then create an instance and give a reference to that instance to both your controllers:
public class Main extends Application {
public Stage primaryStage;
private BorderPane rootLayout;
private final Model model ;
#Override
public void start(Stage primaryStage) throws Exception{
this.model = new Model();
this.primaryStage = primaryStage;
this.primaryStage.setTitle("");
initRootLayout();
showOverview();
}
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();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.setHeight(900);
primaryStage.setWidth(900);
// Give the controller access to the main app.
RootLayoutController controller = loader.getController();
// do you really need your controllers to have access to the main app?
// it seems like it creates excessive coupling (you can no longer
// use this fxml-controller pair anywhere else as it has a
// dependency on this application class)
controller.setMainApp(this);
controller.initModel(model);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showOverview(){
try {
// Load Overview
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/Overview.fxml"));
AnchorPane overview = (AnchorPane) loader.load();
// Set overview into the center of root layout.
rootLayout.setCenter(overview);
// Give the controller access to the main app.
// WHY???
OverviewController controller = loader.getController();
controller.setMainApp(this);
controller.initModel(model);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
And now you can do:
public class OverviewController {
private Model model ;
public void initModel(Model model) {
this.model = model ;
this.model.getPoints().addListener((Change<? extends Point> change) -> {
while (change.next()) {
if (change.wasAdded()) {
for (Point p : change.getAddedSublist()) {
draw(p.x, p.y);
}
}
}
});
}
#FXML
Canvas canvas;
public void test(){
Point point = new Point(5,5);
model.getPoints().add(point);
}
public void draw(double x, double y){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.rgb(255, 0, 0));
gc.fillOval(x-4, y-4, 8, 8);
}
}
And similarly:
public class RootLayoutController {
private Model model ;
public void initModel(Model model) {
this.model = model ;
}
// handler that adds a point:
#FXML
private void addPoint(MouseEvent e) {
Point p = new Point(e.getX(), e.getY());
model.getPoints().add(p);
}
}

JavaFX - How to add Item to ListView from different Stage?

How can i add an item to an already existing ListView from a different Stage (Window).
Basically i just want to add Text to the ListView from Window 2 to the ListView in Window 1.
Thanks in advance.
Sorry i forgot this is what i've got so far in my controller class: (im a beginner in javafx ..)
(below i try to add a String to the ListView but this doesnt work...and i dont know why )
public class ClientGUIController implements Initializable {
#FXML private Text usernameText;
#FXML private Button cancelButtonNewDate;
#FXML private TextField newDateTitel,newDateJahr;
#FXML private TextArea newDateNotiz;
#FXML private ComboBox<String> newDateTag,newDateMonat,newDateStunde,newDateMinute;
#FXML private ListView<String> terminListView;
private ObservableList<String> termine =
FXCollections.observableArrayList();
private ObservableList<String> listItems = FXCollections.observableArrayList("Add Items here");
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public Text getUsernameText() {
return usernameText;
}
public void setUsernameText(String username ) {
this.usernameText.setText(username);
terminListView.setItems(listItems);
listItems.add("test");
}
public void newDate() {
Stage newDate = new Stage();
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("newDate.fxml"));
// FXMLLoader loader = new FXMLLoader();
// root = (Parent) loader.load(getClass().getResource("NewDate.fxml").openStream());
} catch (IOException e) {
e.printStackTrace();
return;
}
Scene sceneNewDate = new Scene(root);
// sceneNewDate.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
newDate.setTitle("Neuer Termin");
newDate.setScene(sceneNewDate);
newDate.show();
}
public void createNewDate() throws IOException {
// Termine meinTermin = new Termine(Integer.parseInt(newDateTag.getValue()), Integer.parseInt(newDateMonat.getValue()), Integer.parseInt(newDateJahr.getText()), newDateTitel.getText(), newDateNotiz.getText(),
// Integer.parseInt(newDateStunde.getValue()), Integer.parseInt(newDateMinute.getValue()));
//Add item to ListView
listItems.add("test"); <- this doesnt work
}
public void closeDialogue(){
Stage stage = (Stage) cancelButtonNewDate.getScene().getWindow();
stage.close();
}
}
One way to do this is to pass listItems to the controller for newDate.fxml, so it can just add to that list. So, assuming the controller class for newDate.fxml is NewDateController, you would do something like:
public class NewDateController {
private ObservableList<String> data ;
public void setData(ObservableList<String> data) {
this.data = data ;
}
// other code as before...
// button handler:
#FXML
private void handleButtonPress() {
data.addItem("test");
}
}
Then in your ClientGUIController, load the fxml like this:
public void newDate() {
Stage newDate = new Stage();
Parent root;
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("newDate.fxml"));
root = loader.load();
NewDateController controller = loader.getController();
controller.setData(listItems);
} catch (IOException e) {
e.printStackTrace();
return;
}
Scene sceneNewDate = new Scene(root);
newDate.setTitle("Neuer Termin");
newDate.setScene(sceneNewDate);
newDate.show();
}

Categories