Javafx combobox not updating dropdown size upon change on realtime?

前端 未结 5 666
一整个雨季
一整个雨季 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: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 list1 = new ArrayList<>();
          list1.add("one");
          list1.add("two");
          list1.add("three");
    
          List list2 = new ArrayList<>();
          list2.add("one");
          list2.add("two");
          list2.add("three");
          list2.add("four");
    
          final ComboBox combo = new ComboBox();
          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);
       }
    
    } 
    

提交回复
热议问题