ListCell不显示自定义项目

时间:2018-10-16 17:41:51

标签: java javafx javafx-8 javafx-2

我正在尝试创建自定义ListCell<Chat>,但是当我填写数据时,主视图上的ListView为空。我正在尝试将Spring Boot和JavaFX结合使用,如此处所述:https://github.com/turais/TuraisJavaFxExamples。当我使用ListView<String>时,它将按预期显示字符串列表,但在尝试自定义实现时不会显示我的自定义ListCell<Chat>

问题出在哪里,或者我错过了第二天找不到的东西...

主控制器:

    public class MainController {

    @FXML
    private TextField txtSearch;

    @FXML
    private ListView<Chat> lvChats;

    private ObservableList<Chat> chatsObservableList;

    @Autowired
    private ListChatCell listChatCell;


    @FXML
    public void initialize() {

    }

    @PostConstruct
    public void init() {
        this.chatsObservableList = FXCollections.observableArrayList(getTestList());

        lvChats.setItems(chatsObservableList);
        lvChats.setCellFactory(studentListView -> listChatCell);
    }

    @FXML
    public void showSettings() {

    }

    private List<Chat> getTestList() {
        List<Chat> chatList = new ArrayList<>();



        chatList.add(new Chat("Last message 1", "Chat 1", "12:09", "3"));
        chatList.add(new Chat("Last message 2", "Chat 2", "14:09", "1"));

        return chatList;
    }
}

ListCell:

    public class ListChatCell extends ListCell<Chat> {

    @FXML
    private Text txtLastMessage;

    @FXML
    private Text txtChatName;

    @FXML
    private Text txtLastMessageTime;

    @FXML
    private Text txtUnreadMessage;

    @FXML
    private Pane pane;


    @FXML
    private ImageView imgChatIcon;


    @FXML
    public void initialize() {

    }


    @Override
    protected void updateItem(Chat chat, boolean empty) {
        super.updateItem(chat, empty);

        if (empty || chat == null) {

            setText(null);
            setGraphic(null);

        } else {

            txtLastMessage.setText(chat.getLastMessage());
            txtChatName.setText(chat.getChatName());
            txtLastMessageTime.setText(String.valueOf(chat.getLastMessageTime()));
            txtUnreadMessage.setText(String.valueOf(chat.getNumberUnreadMessages()));

            imgChatIcon.setImage(new Image("tl.jpeg"));
            setText("help");
            setGraphic(pane);
        }

    }
}

我的ListCell FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Pane fx:id="pane" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.client.manager.fx.ui.cell.ListChatCell">
   <children>
      <HBox prefHeight="200.0" prefWidth="600.0">
         <children>
            <ImageView fx:id="imgChatIcon" fitHeight="200.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
            <GridPane minHeight="-Infinity" prefHeight="200.0" prefWidth="400.0">
               <children>
                  <Text fx:id="txtLastMessage" layoutX="90.0" layoutY="-1.0" strokeType="OUTSIDE" strokeWidth="0.0" text="LastMessageText" GridPane.rowIndex="1" />
                  <Text fx:id="txtChatName" strokeType="OUTSIDE" strokeWidth="0.0" text="ChatName" wrappingWidth="208.2060546875">
                     <font>
                        <Font size="14.0" />
                     </font>
                  </Text>
                  <Text fx:id="txtLastMessageTime" strokeType="OUTSIDE" strokeWidth="0.0" text="TimeLastMessage" GridPane.columnIndex="1" />
                  <Text fx:id="txtUnreadMessage" strokeType="OUTSIDE" strokeWidth="0.0" text="NumberOfUnreadMessages" GridPane.columnIndex="1" GridPane.rowIndex="1" />
               </children>
               <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="321.0" minWidth="10.0" prefWidth="305.0" />
                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="128.0" minWidth="10.0" prefWidth="71.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints maxHeight="189.0" minHeight="10.0" prefHeight="95.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="295.0" minHeight="10.0" prefHeight="105.0" vgrow="SOMETIMES" />
               </rowConstraints>
            </GridPane>
         </children>
      </HBox>
   </children>
</Pane>

主要FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.client.manager.fx.ui.TelegramController">
   <children>
      <SplitPane prefHeight="400.0" prefWidth="200.0">
         <items>
            <VBox prefHeight="323.0" prefWidth="198.0">
               <children>
                  <HBox prefHeight="27.0" prefWidth="200.0">
                     <children>
                        <Button mnemonicParsing="false" onAction="#showSettings" text="Settings" />
                        <TextField fx:id="txtSearch" />
                     </children>
                  </HBox>
                  <ListView fx:id="lvChats" />
               </children>
            </VBox>
         </items>
      </SplitPane>
   </children>
</AnchorPane>

0 个答案:

没有答案