JavaFX combobox not refreshing the number of visible rows

左心房为你撑大大i 提交于 2020-01-03 08:45:15

问题


I'm changing the items on a combo-box dynamically. It's working perfectly, except that the number of visible rows remains fixed according to the first time the combo-box is clicked.

Example: The combobox items are set to A and B. When I click the combobox, it shows 2 rows with A and B. Then I change dynamically the items to C, D and E. When I click the combobox, it shows 2 rows with C and D and a scrollbar.

I already set the

comboBox.setVisibleRowCount(10);

but it keeps showing only 2 rows and a scrollbar.

If I do the opposite, first set the items to C, D and E and click the combobox; it shows the three visible rows. Then I change dynamically the items to A and B. When I click the combobox, it shows 3 rows! A, B and a blank row.


回答1:


There is an issue already submitted in Javafx issue traker. https://javafx-jira.kenai.com/browse/RT-37622

It only works if the combobox has a fixed cell size. I did that with css.

for example:

.combo-box .list-view .list-cell{

-fx-cell-size: 35;

}



回答2:


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.




回答3:


Here's at least a workaround: after changing the number of items, also change visibleRowCount to something else and back to your desired value. This seems to trigger an update of the dropdown height, although it was not always accurate in my tests.

Also, if you change visibleRowCount to 10, effectively nothing happens because this is the initial value and setting it to 10 does not invalidate the property.



来源:https://stackoverflow.com/questions/23094062/javafx-combobox-not-refreshing-the-number-of-visible-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!