List View not updating from separate FXML file JavaFX

43 Views Asked by At

I have an Employee ListView in MainController / Main.fxml that sits in a split pane to the left an anchorpane that loads an EmployeeController / Employee.fxml. When I add a new Employee the debugger in IntelliJ shows the added employee, but the list doesn't refresh.

Model for Employee

public class Employee {
         private String name;
         public static ArrayList<String> employeeList = new ArrayList<>();
    
         public Employee(String name) {
          this.name = name;
    }
    

Initial Start Method

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("Main.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 600, 600 );
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        Employee.employeeList.add("Joe");
        launch();
    }
}

MainController Class where the list is

public class MainController implements Initializable {
    public AnchorPane rootAnchorPane;
    public ListView<String> employeeListView;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        makeEmployeeList();
    }

    private void makeEmployeeList() {
        ObservableList<String> employees = FXCollections.observableArrayList();
        employees.addAll(Employee.employeeList);
        employeeListView.setItems(employees);
    }

    public void loadEmployeeFXML(ActionEvent actionEvent) throws IOException {
        AnchorPane pane = FXMLLoader.load(Objects.requireNonNull(MainController.class.getResource("Employee.fxml")));
        rootAnchorPane.getChildren().setAll(pane);
        Stage stage = (Stage) pane.getScene().getWindow();
        stage.setWidth(600);
        stage.setHeight(600);

    }
}

MainController FXML File

<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/18" fx:controller="MainController">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>

    <SplitPane fx:id="mainSplitPane" dividerPositions="0.5" focusTraversable="true" prefHeight="854.0" VBox.vgrow="ALWAYS">
        <VBox fx:id="employeeVbox" maxWidth="255.0" minWidth="225.0" prefHeight="838.0" prefWidth="225.0">

            <ListView fx:id="employeeListView" maxWidth="255.0" minWidth="225.0" prefHeight="820.0" prefWidth="225.0">
            </ListView>
         <Button mnemonicParsing="false" onAction="#loadEmployeeFXML" text="Load Employee FXML" />
        </VBox>
        <ScrollPane fx:id="mainScrollPane" fitToHeight="true" fitToWidth="true" prefHeight="857.0">
            <AnchorPane id="Content" fx:id="rootAnchorPane" prefHeight="856.0" prefWidth="1090.0">
            </AnchorPane>
        </ScrollPane>
    </SplitPane>
</VBox>

Employee Controller - Adding Employees to the database and refreshing the list in MainController

public class EmployeeController {
    public void addEmployees(ActionEvent actionEvent) throws IOException {
        Employee.employeeList.add("Joe");
        Employee.employeeList.add("Jim");
        refreshEmployeeList();
    }

    private void refreshEmployeeList() throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
        loader.load();
    }
}

Employee FXML

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="EmployeeController">
   <children>
      <Button mnemonicParsing="false" onAction="#addEmployees" text="Add New Employees" />
   </children>

</AnchorPane>

After adding a new Employee to the database, refreshEmployeeList() runs through the initializer in MainController and the debugger shows the new employee at: current.add("FILTERED EMPLOYEES FROM THE DATABASE") and the size is correct at employeeListView.setItems(current) but the List doesn't refresh in the SplitPane View. What am I doing wrong?

0

There are 0 best solutions below