Javafx combobox not updating dropdown size upon change on realtime?

前端 未结 5 556
一整个雨季
一整个雨季 2021-02-09 13:00

I am using Javafx v8.0.25-b18.

The problem I occur is that the size of the dynamic combox\'s dropdown list doesn\'t change, so if I had initially two items in the dropdo

5条回答
  •  难免孤独
    2021-02-09 13:43

    You don't have to change the number of entries to be displayed. The implementation will handle that automatically.

    Say you want to display at most 10 items. Then, you use comboBox.setVisibleRowCount( 10 ); If there are less than 10 items at any time, Javafx will only show as many rows as there are items.

    Actually, changing the number of visible rows at runtime can sometimes cause errors, from my experience, so you are better of with just having a set number.

    Hope that helps.


    I have some problems understanding what the problem is. I made a short example bellow, can you try it and then say what it doesn't do that you want to do.

    public class Test extends Application{
    
        private int index = 0;
    
        @Override
        public void start(Stage primaryStage) throws IOException{
    
            VBox vbox =  new VBox();    
            vbox.setSpacing(10);
            vbox.setAlignment(Pos.CENTER);
    
            ComboBox box = new ComboBox<>();
            box.setPrefWidth(200);
            box.setVisibleRowCount(10);
    
            Button add = new Button("Add");
            Button remove = new Button("Remove");
    
            add.setOnAction(    new EventHandler() {
                @Override
                public void handle(ActionEvent event) {
                    box.getItems().add("Item " + index++);
                }
            });
    
    
            remove.setOnAction( new EventHandler() {       
                @Override
                public void handle(ActionEvent event) {
                    if( index > 0 )
                        box.getItems().remove(--index);
                }
            });
    
    
            vbox.getChildren().addAll(add, remove, box);
    
    
            Scene scene = new Scene(vbox);
    
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

提交回复
热议问题