gpt4 book ai didi

JavaFX 组合框无限调用 listcell.updateitem

转载 作者:行者123 更新时间:2023-12-02 11:20:48 25 4
gpt4 key购买 nike

我正在尝试制作一个包含大量项目(超过 10000)的组合框。它初始化没有问题。但当我点击它时,它就卡住了。为了调试,我创建了自己的 listCell 并遵循 updateitem 函数。当我点击时,它会无限调用 updateitem 。它不应该只更新可见项目吗?这是一个示例 Controller :

   package sample;

import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;


public class Controller {
@FXML
ComboBox comboBox1;

public final class ExampleCell<T> extends ListCell<T> {

Label myLabel;

@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item,empty);
System.out.println("update");
if (empty) {
setGraphic(null);
} else {
if(myLabel==null){
myLabel=new Label((String)item);

}else{
myLabel.setText((String)item);}
setGraphic(myLabel);
}
}
}
public void initialize(){

for(int i =0;i<10000;i++){
comboBox1.getItems().add("example");
}
comboBox1.setCellFactory(param -> new ExampleCell<>());
}
}

还有我的 fxml

<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.ComboBox?>
<GridPane fx:controller="sample.Controller"
prefWidth="500"
prefHeight="500"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<ComboBox
fx:id="comboBox1"
/>
</GridPane>

最佳答案

原因确实是所有单元格的首选宽度的测量,如 ComboBoxListViewSkin 中记录的(未公开!):

// By default we measure the width of all cells in the ListView. If this
// is too burdensome, the developer may set a property in the ComboBox
// properties map with this key to specify the number of rows to measure.
// This may one day become a property on the ComboBox itself.
private static final String COMBO_BOX_ROWS_TO_MEASURE_WIDTH_KEY = "comboBoxRowsToMeasureWidth";

出路是限制应该测量的行数 - 没有保证,因为没有公共(public)文档根本就没有规范:

comboBox1.getProperties().put("comboBoxRowsToMeasureWidth", 10);

最初仍然多次调用 updateItem,即至少是测量期间给定限制的两倍(填充和释放)加上一次设置实际值。

关于JavaFX 组合框无限调用 listcell.updateitem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49930449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com