Customize ListView in JavaFX with FXML

前端 未结 3 615
無奈伤痛
無奈伤痛 2020-11-29 22:05

I want to make a customize list view in javafx. Here I need to bind multiple component in list cell as follow, like one label, one textfield, one button under one HBox and

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 22:26

    The example above by @Anvay needs a couple of tweaks to work. These are simple things to set on-track.

    1. The ListViewController needs to be running on the JavaFX application thread.
    2. You can only call the injected @FXML elements from the JavaFX initialize() method
    3. Need to call setListView()
    4. The stringSet in the example needs to be allocated with a new before calling setListView().

    The ListViewController below works with these changes. I changed "stringSet" to a list, "stringList". The controller is pretty much the sample controller provided by Scene Builder 2

     public class ListViewController 
     {
    
         @FXML private   ResourceBundle      resources;
    
         @FXML private   URL                 location;
    
         @FXML private   ListView            listView;
    
         private         List        stringList     = new ArrayList<>(5);
         private         ObservableList      observableList = FXCollections.observableArrayList();
    
         public void setListView(){
    
             stringList.add("String 1");
             stringList.add("String 2");
             stringList.add("String 3");
             stringList.add("String 4");
    
             observableList.setAll(stringList);
    
             listView.setItems(observableList);
    
             listView.setCellFactory(
                 new Callback, javafx.scene.control.ListCell>() {
                     @Override
                     public ListCell call(ListView listView) {
                         return new ListViewCell();
                     }
                 });
         }
    
         @FXML
         void initialize() {
             assert listView != null : "fx:id=\"listView\" was not injected: check your FXML file 'CustomList.fxml'.";
    
             setListView();
         }
    
     }//ListViewController
    

    The JavaFX platform needs to be started in the main() method from a JavaFX Application. Netbeans conviently provides most of this structure from the Maven JavaFX application template.

    public class MainApp extends Application {
    
        @Override
        public void start(Stage stage) throws Exception {
    
            Parent root = FXMLLoader.load(getClass().getResource("/fxml/CustomList.fxml"));
    
            Scene scene = new Scene(root);
            scene.getStylesheets().add("/styles/Styles.css");
    
            stage.setTitle("CustomList");
            stage.setScene(scene);
            stage.show();
        }
    
        /**
         *  The main() method is ignored in correctly deployed JavaFX application.
         * 
         *  @param args the command line arguments
         **/
        public static void main(String[] args) {
    
            launch(args);
        }
    }
    

提交回复
热议问题