简体   繁体   中英

Problems with list view after updating to java 8

After updating to Java 8, i got problems displaying content of list view

  public class TestController extends AnchorPane implements Initializable {

        @FXML
        private ListView<String> listView;

        @Override
        public void initialize(URL url, ResourceBundle rb) {

        }

        public void setApp() {
            setContent();
        }

        /**
         * Set content in list
         */
        private void setContent() {
            ObservableList<String> items = FXCollections.observableArrayList();
            items.add("1");
            items.add("2");


            listView.setItems(items);

        }
    }

With Java 7 this gives me a list with values. After update to java 8, it gives me a empty frame.

Thanks James_D, here is the code you asked for

public void goToTest() {
            try {
                TestController test = (TestController) replaceSceneContent("/fxml/test.fxml");
                test.setApp();

            } catch (Exception ex) {
                System.out.println("Error loading: " + ex + " - " + ex.getMessage());
            }
        }

     private Initializable replaceSceneContent(String fxml) throws Exception {
            FXMLLoader loader = new FXMLLoader();
            InputStream in = MainApp.class.getResourceAsStream(fxml);
            loader.setBuilderFactory(new JavaFXBuilderFactory());
            loader.setLocation(MainApp.class.getResource(fxml));
            AnchorPane page;
            try {
                page = (AnchorPane) loader.load(in);
            } finally {
                in.close();
            }

            Scene scene = new Scene(page);
            stage.setScene(scene);
            stage.sizeToScene();
            stage.setResizable(resizable);
            return (Initializable) loader.getController();
        }
listView = new ListView<>(items);

will not work; it will create a new ListView and populate it, whereas what you want to do is populate the ListView you created in your FXML.

listView.setItems(items);

is the correct code.

(I assume you really have public class TestController implements Initializable , not public class test implements Initializable .)

So the following should work:

private void setContent() {
    ObservableList<String> items = FXCollections.observableArrayList();
    items.add("1");
    items.add("2");

    listView.setItems(items);

}

Your FXMLLoader code is a little unusual in that you set a location (URL) but then use the load(InputStream) method to load. I'd be surprised if this fixed it, but the usual way would be:

    private Initializable replaceSceneContent(String fxml) throws Exception {
            FXMLLoader loader = new FXMLLoader();
            // not needed:
            // InputStream in = MainApp.class.getResourceAsStream(fxml);

            // Not really needed, this is the default:
            loader.setBuilderFactory(new JavaFXBuilderFactory());
            loader.setLocation(MainApp.class.getResource(fxml));
            AnchorPane page;

            // instead of this:
//            try {
//                page = (AnchorPane) loader.load(in);
//            } finally {
//                in.close();
//            }

            // just this (since you have set the location):
            page = loader.load();

            Scene scene = new Scene(page);
            stage.setScene(scene);
            stage.sizeToScene();
            stage.setResizable(resizable);
            return (Initializable) loader.getController();
        }

(Random guess: did you accidentally make listView static in the Controller?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM