2

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?

dan_jen1
  • 21
  • 4
  • 3
    Provide a [mcve], make it minimal (no db access, stub that out it is not relevant and only provide minimal code to show the problem), complete (you need the fxml and the program should run via copy and paste with no change or addition) and it should replicate the issue. – jewelsea May 08 '22 at 20:41
  • Make sure you're adding the element to the same list view _instance_ that's being displayed in the UI. If that's not the problem, then please provide a [mre] as previously suggested. Note you can [edit] your question. – Slaw May 08 '22 at 21:10
  • First post - thanks for the feedback. Hopefully this is more clear. – dan_jen1 May 08 '22 at 22:12
  • 2
    Each time you load `Main.fxml` a new `ListView` is created. So you update a list view that isn’t displayed. I recommend using a MVC approach, see, eg https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – James_D May 08 '22 at 23:25
  • 1
    and don't use static scope – kleopatra May 09 '22 at 07:31
  • @James_D thanks the shared link was very helpful. – dan_jen1 May 10 '22 at 22:48

0 Answers0