Javafx组合框是否在实时更改时不更新下拉列表的大小? [英] Javafx combobox not updating dropdown size upon change on realtime?
问题描述
我正在使用Javafx v8.0.25-b18.
我出现的问题是动态combox的下拉列表的大小不会更改,因此如果我最初在下拉列表中有两个项目,则该下拉列表大小将适合两个项目,但是如果我现在填充动态的combox,其中包含三个项目,然后我会在里面看到一个小的滚动条!?,如果我删除一个项目,则combox中将有一个空格!
我想在每次输入值时都重置"下拉列表的大小,因此每次在运行时填充时,下拉列表的大小都是正确的.
为了澄清更多,我添加了三个图像:
1.第一个屏幕截图显示初始下拉菜单大小为2
- 第二张屏幕截图显示了同一个combox,现在在运行时我要添加2个值,我希望它现在具有一个下拉列表,其大小为4,但是下拉列表的大小保持为2,并且仅添加了不需要的滚动条
- 最后一个屏幕截图是当我删除项目并且combox中只剩下一个项目时,我希望看到下拉菜单项为1,但是不幸的是,我看到的下拉菜单大小为2,因此是空白而不是第二项
我正在添加用于创建此场景的简单代码,感谢@Gikkman的帮助,而实际上代码就是他的!
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);
}
}
尝试一下:
box.hide(); //before you set new visibleRowCount value
box.setVisibleRowCount(rows); // set new visibleRowCount value
box.show(); //after you set new visibleRowCount value
它可与可编辑的comboBox配合使用,我认为它可以在您的情况下使用.
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
- 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
- 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
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);
}
}
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.
这篇关于Javafx组合框是否在实时更改时不更新下拉列表的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!