0

I making a productivity app for school. So I basically got a Scene switcher and it is reloading the .fxml file each time. So whatever values were stored in taskListView are now gone. I am new to java fx, and can someone help me fix this?

HelloApplication.java:

package com.example.newapp;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

import java.util.Objects;

public class HelloApplication extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("scene2.fxml"));
        Parent root = loader.load();

        String cssFile = Objects.requireNonNull(getClass().getResource("style.css")).toExternalForm();
        root.getStylesheets().add(cssFile);

        Pane pane = new Pane(root);

        // Create a circle
        Circle circle = new Circle(8, Color.BLACK);
        circle.setMouseTransparent(true);
        pane.getChildren().add(circle);

        // Update the circle's position based on mouse movement
        pane.setOnMouseMoved(event -> {
            circle.setCenterX(event.getX());
            circle.setCenterY(event.getY());
        });

        primaryStage.setTitle("To-Do List");
        primaryStage.setScene(new Scene(pane));
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

HelloController.java code:

package com.example.newapp;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Objects;

public class HelloController {

    private Stage primaryStage;
    private Scene scene;
    private Parent scene1Root;
    private Parent scene2Root;

    @FXML
    private ListView<String> taskListView;

    @FXML
    private TextField taskTextField;

    @FXML
    private void addTask() {
        String task = taskTextField.getText();
        if (!task.isEmpty()) {
            taskListView.getItems().add(task);
            taskTextField.clear();
        }
    }

    @FXML
    private void removeTask() {
        ObservableList<String> selectedTasks = taskListView.getSelectionModel().getSelectedItems();
        taskListView.getItems().removeAll(selectedTasks);
    }

    public void switchToScene1(ActionEvent event) throws IOException {
        if (scene1Root == null) {
            scene1Root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("scene1.fxml")));
        }
        primaryStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        scene = new Scene(scene1Root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void switchToScene2(ActionEvent event) throws IOException {
        if (scene2Root == null) {
            scene2Root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("scene2.fxml")));
        }
        primaryStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        scene = new Scene(scene2Root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

I have tried to load the .fxml separately, but it was not working for me it still kept clearing all the data in the taskListView.

0 Answers0