JavaFX添加自定义ViewCell不会出现

时间:2014-08-22 13:50:33

标签: java javafx javafx-2 fxml

我是JavaFX的新手,想创建一个自定义listView,每个ViewCell包含3个标签和2个图像,我已经在fxml文件中定义了它。为简单起见,我在下面仅使用2个标签发布了简化版本。

我认为它与我的fxml文件中的根对象有关,但这只是猜测。提前致谢。请参阅下面的代码

ListView<MyObject> listView = new ListView<MyObject>();
List<MyObject> listOfObjects = new ArrayList<MyObject>();
ObservableList<MyObject> myObservableList = FXCollections.observableList(listOfObjects);
listView.setItems(myObservableList);
listView.setCellFactory(new Callback<ListView<MyObject>, ListCell<MyObject>>(){
    @Override
    public ListCell<MyObject> call(ListView<MyObject> list) {
        return new MyViewCell(); // this is my custom ViewCell
    }
});

MyViewCell

public class MyViewCell extends ListCell<MyObject> {

    @FXML 
    private Label label1;

    @FXML 
    private Label label2;

    public MyViewCell() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/MyObjectViewCell.fxml"));
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (Exception e) {
        e.printStackTrace();
        }
    }

    @Override
    protected void updateItem(MyObject t, boolean bln) {
        super.updateItem(t, bln);

        // if i call this.setText("Some text"); then this appears in the listView       

        if (t != null) {
            label1.setText("Label 1");
            label2.setText("Label 2");

            this.visibleProperty.set(true);
        } else {
            this.visibleProperty.set(false);

        }
    }
}

现在我的fxml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.scene.image.*?>
    <?import javafx.geometry.*?>
    <?import javafx.scene.text.*?>
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>

    <GridPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="70.0"  prefWidth="240.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/8"     xmlns:fx="http://javafx.com/fxml/1">
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="195.0" minWidth="10.0"   prefWidth="188.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="97.0" minWidth="19.0"  prefWidth="67.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="106.0" minWidth="10.0" prefWidth="106.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="96.0" minWidth="19.0"  prefWidth="25.0" />
        <ColumnConstraints hgrow="ALWAYS" maxWidth="130.0" minWidth="60.0"  prefWidth="130.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      </rowConstraints>
       <children>
          <Label fx:id="label1" text="Adult single ticket" textFill="WHITE"     GridPane.valignment="TOP">
             <font>
                <Font name="System Bold" size="12.0" />
             </font>
      </Label>
      <Label fx:id="label2" text="0" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="TOP">
         <font>
            <Font name="System Bold" size="12.0" />
         </font>
      </Label>
   </children>
   <padding>
      <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
   </padding>
</GridPane>

1 个答案:

答案 0 :(得分:2)

您不会对从FXML加载的Node层次结构执行任何操作。

MyViewCell添加字段:

private Parent graphic ;

并在构造函数中将其初始化为load()调用的结果:

try {
    graphic = fxmlLoader.load();
} catch (Exception e) 
  // ...
}

然后,您的updateItem(...)应为:

protected void updateItem(MyObject t, boolean bln) {
    super.updateItem(t, bln);

    if (t != null) {
        label1.setText("Label 1");
        label2.setText("Label 2");

        setGraphic(graphic);
    } else {
        setGraphic(null);
    }
}

你也应该在setContentDisplay(ContentDisplay.GRAPHIC_ONLY);的构造函数中调用MyViewCell