ListView JavaFX中的自定义项
我对JavaFX中的ListView有疑问 如何在ListView中执行自定义项,如下所示:ListView JavaFX中的自定义项,java,image,listview,javafx,Java,Image,Listview,Javafx,我对JavaFX中的ListView有疑问 如何在ListView中执行自定义项,如下所示: 其次,当我单击按钮1时,我想显示image2和textfield1,实现您自己的CellFactory将为您提供所有要应用于列表视图的单元格的选项。不幸的是,你上传的图像没有打开,所以我无法理解你的确切要求。无论如何,这就是如何为列表视图设置CellFactory,其中T是您的数据类型 ListView#setCellFactory(Callback<ListView<T>, Li
其次,当我单击按钮1时,我想显示image2和textfield1,实现您自己的
CellFactory
将为您提供所有要应用于列表视图的单元格的选项。不幸的是,你上传的图像没有打开,所以我无法理解你的确切要求。无论如何,这就是如何为列表视图设置CellFactory
,其中T
是您的数据类型
ListView#setCellFactory(Callback<ListView<T>, ListCell<T>> value)
ListView#setCellFactory(回调值)
示例:
ListView<Employee> listView = new ListView<>();
listView.setCellFactory(new Callback<ListView<Employee>, ListCell<Employee>>() {
@Override
public ListCell<Employee> call(ListView<Employee> param) {
return new ListCell<Employee>() {
private ImageView imageView = new ImageView("ImageURL");
private TextField textField = new TextField("Text");
private Button button = new Button("Button");
private BorderPane bp = new BorderPane(imageView, null, button, null, textField);
@Override
protected void updateItem(Employee item, boolean empty) {
if (item == null || empty) {
setText(null);
setGraphic(null);
} else {
setText(item.getName());
setGraphic(bp);
}
}
};
}
});
ListView ListView=新建ListView();
setCellFactory(新回调(){
@凌驾
公共ListCell调用(ListView参数){
返回新的ListCell(){
私有ImageView=新的ImageView(“ImageURL”);
私有文本字段TextField=新文本字段(“文本”);
专用按钮=新按钮(“按钮”);
private BorderPane bp=新的BorderPane(图像视图,null,按钮,null,文本字段);
@凌驾
受保护的void updateItem(员工项,布尔值为空){
如果(项==null | |空){
setText(空);
设置图形(空);
}否则{
setText(item.getName());
设定图形(bp);
}
}
};
}
});
实现您自己的CellFactory
为您提供了要应用于列表视图
的单元格的所有选项。不幸的是,你上传的图像没有打开,所以我无法理解你的确切要求。无论如何,这就是如何为列表视图设置CellFactory
,其中T
是您的数据类型
ListView#setCellFactory(Callback<ListView<T>, ListCell<T>> value)
ListView#setCellFactory(回调值)
示例:
ListView<Employee> listView = new ListView<>();
listView.setCellFactory(new Callback<ListView<Employee>, ListCell<Employee>>() {
@Override
public ListCell<Employee> call(ListView<Employee> param) {
return new ListCell<Employee>() {
private ImageView imageView = new ImageView("ImageURL");
private TextField textField = new TextField("Text");
private Button button = new Button("Button");
private BorderPane bp = new BorderPane(imageView, null, button, null, textField);
@Override
protected void updateItem(Employee item, boolean empty) {
if (item == null || empty) {
setText(null);
setGraphic(null);
} else {
setText(item.getName());
setGraphic(bp);
}
}
};
}
});
ListView ListView=新建ListView();
setCellFactory(新回调(){
@凌驾
公共ListCell调用(ListView参数){
返回新的ListCell(){
私有ImageView=新的ImageView(“ImageURL”);
私有文本字段TextField=新文本字段(“文本”);
专用按钮=新按钮(“按钮”);
private BorderPane bp=新的BorderPane(图像视图,null,按钮,null,文本字段);
@凌驾
受保护的void updateItem(员工项,布尔值为空){
如果(项==null | |空){
setText(空);
设置图形(空);
}否则{
setText(item.getName());
设定图形(bp);
}
}
};
}
});
设置自定义单元格工厂并更新从其updateItem
方法返回的单元格。(将适当的内容设置为图形
属性。)可能的重复好的,如果我想使用jpanel而不是image2怎么办?请阅读Oracle的cell factory官方指南。设置自定义cell factory并更新它从updateItem
方法返回的单元格。(将适当的内容设置为graphic
属性。)可能的重复好的,如果我想使用jpanel而不是image2怎么办?请阅读Oracle的cell factory官方指南。我想在ListView中的一个项目上使用按钮、图像视图、文本视图不要在每次调用updateItem时都重新创建控件(通常称为reaaaally)我只是想举一个例子,说明如何使用graphic
属性在单元格中包含控件。谢谢:)就是这样:)@kleopatra给出一个很好的示例,说明如何实现它,而不是只给出负面反馈。你的方式对社区没有帮助。我希望在ListView中的一个项目上有按钮、图像视图、文本视图不要在每次调用updateItem时都重新创建控件(通常称为reaaaally)我只是想举一个例子,说明如何使用graphic
属性在单元格中包含控件。谢谢:)就是这样:)@kleopatra举一个很好的例子说明如何实现它,而不是只给出负面反馈。你的方式无助于社区。