我正在使用JavaFX ListView组件,并希望使用自定义单元工厂。打开包含列表视图的对话框时,将正确呈现项目。但是,如果我尝试从基础可观察列表中添加或删除项目,则列表视图中的项目将无法正确呈现。我将完全重复或遗漏细胞。这是我的自定义单元格的FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import org.controlsfx.glyphfont.Glyph?>
<VBox fx:id="vbox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="150.0" prefWidth="300.0" spacing="5.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<children>
<Label fx:id="nameLabel" style="-fx-font-weight: bold; -fx-font-size: 14;" text="Mr Stephen Lowe" />
<Label fx:id="mobileLabel" text="07798933862">
<graphic>
<Glyph fontFamily="FontAwesome" icon="PHONE" />
</graphic>
<font>
<Font size="12.0" />
</font>
</Label>
<Label fx:id="emailLabel" text="stephen.lowe@mypostoffice.co.uk">
<graphic>
<Glyph fontFamily="FontAwesome" icon="ENVELOPE" />
</graphic>
<font>
<Font size="12.0" />
</font>
</Label>
<Label fx:id="churchMemberLabel" text="Church Member" textFill="#388e3c">
<font>
<Font size="12.0" />
</font></Label>
</children>
</VBox>
这是我的自定义ListCell子类:
/**
*
* @author Stephen
*/
public class MemberListViewCell extends ListCell<MemberEntity> {
@FXML
private Label nameLabel;
@FXML
private Label mobileLabel;
@FXML
private Label emailLabel;
@FXML
private Label churchMemberLabel;
@FXML
private VBox vbox;
private FXMLLoader loader;
public MemberListViewCell() {
}
@Override
protected void updateItem(MemberEntity item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
if (loader == null) {
loader = new FXMLLoader(getClass().getResource("/fxml/listcell.fxml"));
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Open FXML");
alert.setHeaderText("FXML Error");
String msg = "Could not load FXML for list cell factory. Inform software engineer. Terminating program.";
msg += ex.toString();
alert.setContentText(msg);
alert.showAndWait();
System.exit(1);
}
}
nameLabel.setText(item.getName().getFormattedName(Name.NameFormat.FORENAME_SURNAME));
mobileLabel.setText(item.getMobileTelephone());
emailLabel.setText(item.getEmailAddress());
if (item.isChurchMember()) {
churchMemberLabel.setText("Church Member");
churchMemberLabel.setTextFill(Paint.valueOf("#388e3c"));
} else {
churchMemberLabel.setText("Not Church Member");
churchMemberLabel.setTextFill(Paint.valueOf("#d32f2f"));
}
setText(null);
setGraphic(vbox);
}
}
}
答案 0 :(得分:2)
由于您为非空单元格设置了图形,因此如果单元格变空,则需要将其设置为null:
{{1}}