Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx combobox not updating dropdown size upon change on realtime?

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 dropdown, then the dropdown size will be good for two items, but if I now populate the dynamic combox with three items then I get a small scrollbar inside!?, If I remove an item - I will have a blank space in the combox !?

I want to "reset" the dropdown size each time I put values into it, so it will be the right size each time it gets populated at runtime.

To clarify even more I am adding three images:
1. The first screenshot shows the initial dropdown size of 2

Initial dropdown size of 2

  1. The second screenshot shows the same combox, where now at runtime I am adding 2 values, I EXPECT it to have now a dropdown with the size of 4, but instead the dropdown size stays 2 and only adds an unwanted scrollbar

enter image description here

  1. Last screenshot is when I remove items and only one item remains in the combox, I EXPECT to see a dropdown of 1 item, but instead I unfortunately see a dropdown the size of 2 thus an empty space instead of the second item

enter image description here

I am adding the simple code to create this scenario, I want to thank @Gikkman that helped getting this far and the code is actually his!

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);

final 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++);
    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);
}
}
like image 960
Chaiavi Avatar asked Aug 25 '15 08:08

Chaiavi


People also ask

What is combobox in JavaFX?

JavaFX ComboBox class inherited from ComboBoxBase interface. ComboBox shows a list of items from which we can select any one item at a time. Combo Box is helpful when the number of items from the drop-down list exceeds the actual limit then the scrolling option appears.

How does setValue work in combobox?

When you call the setValue method on the ComboBox object, the selected item of the selectionModel property changes to this value even if the value is not in the combo box items list. If the items list then changes to include this value, the corresponding item becomes selected.

How to make a combobox editable in Java?

An editable combo box perfectly fits this task. Use the setEditable(true) method of the ComboBox class to make a combo box editable. With the setPromptText method, you can specify the text to appear in the combo box editing area when no selection is performed.

How do I set the width of a combo box item?

The width of each combo box item is set through the setPrefWidth method. The updateItem method sets the red color for the High and Highest items, green color for the Low and Lowest items, and leaves the Normal item black. Figure 14-7 shows the items of the priority combo box after the cell factory in Example 14-5 is applied.


2 Answers

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.

like image 179
Khrystyna Makar Avatar answered Nov 02 '22 05:11

Khrystyna Makar


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();
  }
});
like image 39
Aref Hoseinikia Avatar answered Nov 02 '22 06:11

Aref Hoseinikia