Javafx combobox not updating dropdown size upon change on realtime?

前端 未结 5 550
一整个雨季
一整个雨季 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:30

    Try this:

    box.hide(); //before you set new visibleRowCount value
    box.setVisibleRowCount(rows); // set new visibleRowCount value
    box.show(); //after you set new visibleRowCount value
    

    It's works for me with editable comboBox and I think it will work in your case.

    0 讨论(0)
  • 2021-02-09 13:31

    I had same problem and I solved it with a quick trick. Just try to show and immediately hide !

    add.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent event) {
        box.getItems().add("Item " + index++);
        box.getItems().add("Item " + index++);
        box.show();
        box.hide();
      }
    });
    
    0 讨论(0)
  • 2021-02-09 13:36

    You can use two JavaFx list. First one is previous com box list, another one is final combo box list. then you can change dynamically using yourCombo.getItems().setAll(Your List);

    Here is my sample code:

    import java.util.ArrayList;
    import java.util.List;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ComboBox;
    import javafx.scene.layout.Region;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class ComboBoxTest extends Application {
    
       @Override
       public void start(final Stage primaryStage) throws Exception {
    
          primaryStage.centerOnScreen();
          primaryStage.setHeight(200);
          primaryStage.setWidth(300);
    
          List<String> list1 = new ArrayList<>();
          list1.add("one");
          list1.add("two");
          list1.add("three");
    
          List<String> list2 = new ArrayList<>();
          list2.add("one");
          list2.add("two");
          list2.add("three");
          list2.add("four");
    
          final ComboBox<String> combo = new ComboBox<String>();
          combo.getItems().setAll(list1);
    
    
          Button button = new Button("Change combo contents");
          button.setOnAction(event -> {
             if ( combo.getItems().size() == 3 ) {
                combo.getItems().setAll(list2);
             } else {
                combo.getItems().setAll(list1);
             }
             combo.show();
          });
    
          VBox box = new VBox(20, combo, button );
          box.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
          primaryStage.setScene(new Scene( new StackPane(box) ));
    
          primaryStage.show();
    
       }
    
       public static void main(String[] args) throws Exception {
          launch(args);
       }
    
    } 
    
    0 讨论(0)
  • 2021-02-09 13:42

    Just like to offer my two cents here. You may add the following codes to your combobox which define a custom listview popup that has variable height according to the current number of items. You can tweak the maximum number of items to be displayed in the popup.

    yourComboBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> param) {
                ListCell cell = new ListCell<String>() {
                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        int numItems = getListView().getItems().size();
                        int height = 175;    // set the maximum height of the popup
                        if (numItems <= 5) height = numItems * 35;    // set the height of the popup if number of items is equal to or less than 5
                        getListView().setPrefHeight(height);
                        if (!empty) {
                            setText(item.toString());
                        } else {
                            setText(null);
                        }
                    }
                };
                return cell;
            }
        });
    
    0 讨论(0)
  • 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<String> box = new ComboBox<>();
            box.setPrefWidth(200);
            box.setVisibleRowCount(10);
    
            Button add = new Button("Add");
            Button remove = new Button("Remove");
    
            add.setOnAction(    new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    box.getItems().add("Item " + index++);
                }
            });
    
    
            remove.setOnAction( new EventHandler<ActionEvent>() {       
                @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);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题