gpt4 book ai didi

java - 如何在 ListView 中使用自定义单元格下载 fxml?

转载 作者:行者123 更新时间:2023-11-30 07:24:39 24 4
gpt4 key购买 nike

我想在ListView中创建一个自定义单元格。请原谅我糟糕的英语!我想在ListView中显示图片、名称和状态。为此,我使用包含 Hbox 的不同 Fxml

  public class Controller {    
CollectionContactForListCollection contactForList = new CollectionContactForListCollection();
@FXML
private ListView<Contact> listContact ;
@FXML
HBox hbox;
@FXML
ImageView avatar;
@FXML
Label labelName;
@FXML
Label lblStatus;
@FXML
Label lblSense;

@FXML
private void initialize(){
contactForList.fieldData();
// listContact.setItems((ObservableList) contactForList.getContactList());
listContact.setCellFactory(new Callback<ListView<Contact>, ListCell<Contact>>() {
@Override
public ListCell<Contact> call(ListView<Contact> param) {
ListCell<Contact> listCell = new ListCell<Contact>() {
@Override
protected void updateItem(Contact item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
//This method does not work download
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/boxInContact.fxml"));
fxmlLoader.setController(this);
labelName.setText(item.getName());
lblSense.setText(item.getSense());
lblStatus.setText(item.getStatus());
avatar.setImage(item.getImage());
}
}
};
return listCell;
}
});
listContact.setItems((ObservableList) contactForList.getContactList());
}

最佳答案

作为一般规则,您应该为每个 FXML 文件使用不同的 Controller 类。使用您拥有的代码,所有单元格都使用相同的 Controller 实例,因此每个控件只有一个引用,即使有许多 labelName 等(每个单元格一个)。

因此,为列表单元格定义的 FXML 定义一个 Controller ,并定义更新控件所需的方法:

public class ContactCellController {

@FXML
private Label labelName ;
@FXML
private Label labelStatus ;
@FXML
private Label labelSense ;
@FXML
private ImageView avatar ;

public void setName(String name) {
labelName.setText(name);
}

public void setStatus(String status) {
labelStatus.setText(status);
}

public void setSense(String sense) {
labelSense.setText(sense);
}

public void setAvatarImage(Image image) {
avatar.setImage(image);
}
}

更新 FXML 文件以使用 fx:controller="my.package.ContactCellController" 的 Controller 属性,然后您的单元实现如下所示

listContact.setCellFactory(lv -> new ListCell<Contact>() {
private Node graphic ;
private ContactCellController controller ;

{
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/boxInContact.fxml"));
graphic = loader.load();
controller = loader.getController();
} catch (IOException exc) {
throw new RuntimeException(exc);
}
}

@Override
protected void updateItem(Contact contact, boolean empty) {
super.updateItem(contact, empty);
if (empty) {
setGraphic(null);
} else {
controller.setName(contact.getName());
controller.setStatus(contact.getStatus());
controller.setSense(contact.getSense());
controller.setAvatarImage(contact.getImage());
setGraphic(graphic);
}
}
});

关于java - 如何在 ListView 中使用自定义单元格下载 fxml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36985517/

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