Java 在ListView中自定义单元格
我已经学习了如何自定义listview的单元格的教程,它的结构是在FXML中定义的。我像往常一样定义cellFactory: setCellFactory((ListView studentListView)->new-SwatchCell()) 使用定义如下的SwatchCell类:Java 在ListView中自定义单元格,java,listview,javafx,Java,Listview,Javafx,我已经学习了如何自定义listview的单元格的教程,它的结构是在FXML中定义的。我像往常一样定义cellFactory: setCellFactory((ListView studentListView)->new-SwatchCell()) 使用定义如下的SwatchCell类: package ui; import java.io.*; import javafx.fxml.*; import javafx.scene.control.*; import javafx.scene.la
package ui;
import java.io.*;
import javafx.fxml.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import logic.*;
public class SwatchCell extends ListCell<ColorSwatch>{
private FXMLLoader fxmlLoader;
@FXML private Label name, tagLine;
@FXML private Circle color1, color2, color3, color4, color5;
@FXML private VBox cellRootNode;
@Override
protected void updateItem(ColorSwatch item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
if (fxmlLoader == null) {
loadFXML();
}
name.setText(item.getName());
String tl = "";
for (Tag t : item.getTagLine()) {
if ("".equals(tl)) {
tl += t.getDescription();
} else {
tl += ", " + t.getDescription();
}
}
this.tagLine.setText(tl);
color1.setFill(item.getColors()[0]);
color2.setFill(item.getColors()[1]);
color3.setFill(item.getColors()[2]);
color4.setFill(item.getColors()[3]);
color5.setFill(item.getColors()[4]);
setText(null);
setGraphic(cellRootNode);
}
}
private void loadFXML() {
fxmlLoader = new FXMLLoader(getClass().getResource("SwatchCellGraph.fxml"));
fxmlLoader.setController(this);
try {
cellRootNode = fxmlLoader.load();
} catch (IOException e) {
System.err.println("failed to load Cell FXML");
System.exit(0);
}
}
}
。我希望我包括了所有相关的代码
我要诚实地说,我不完全理解回调和CellFactory是如何详细工作的,这是我第一次以这种方式改变列表单元格。尽管我知道我不应该问“为我解决这个问题”,但对我来说,学习东西的最好方法是学习工作实例。很明显,我想让ListView的单元格采用FXML文件的外观。谢谢你的帮助
PS:所有FXML代码都是在场景生成器中生成的,不会在NetBeans中弹出任何错误,因此在语法上应该是良好的。FX:应该正确分配ID
主窗口的FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.geometry.Rectangle2D?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<StackPane fx:id="background" maxHeight="1.7976931348623157E308" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="275.0" stylesheets="@MainWindowCSS.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ui.MainWindowGraphController">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<children>
<VBox fx:id="content" alignment="TOP_CENTER" maxHeight="1.7976931348623157E308" maxWidth="-Infinity" prefWidth="265.0" spacing="5.0">
<children>
<HBox prefHeight="32.0" prefWidth="200.0" spacing="5.0">
<children>
<ImageView fitHeight="34.0" fitWidth="170.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/header.png" />
</image>
<viewport>
<Rectangle2D height="32.0" width="170.0" />
</viewport>
</ImageView>
<Button fx:id="miniButton" contentDisplay="GRAPHIC_ONLY" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#minimize" prefHeight="32.0" prefWidth="32.0" styleClass="util-button" text="_">
<graphic>
<ImageView fitHeight="32.0" fitWidth="32.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/minimize.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets bottom="-5.0" left="-5.0" right="-5.0" top="-5.0" />
</padding>
</Button>
<Button fx:id="closeButton" contentDisplay="GRAPHIC_ONLY" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#kill" prefHeight="32.0" prefWidth="32.0" styleClass="util-button" text="x">
<graphic>
<ImageView fitHeight="32.0" fitWidth="32.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/cross.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets bottom="-5.0" left="-5.0" right="-5.0" top="-5.0" />
</padding>
</Button>
</children>
</HBox>
<Separator maxHeight="0.0" minHeight="0.0" prefHeight="0.0" prefWidth="177.0" />
<ComboBox fx:id="comboBox" focusTraversable="false" maxWidth="1.7976931348623157E308" onAction="#repopulateListView" prefHeight="33.0" prefWidth="284.0" VBox.vgrow="ALWAYS" />
<HBox spacing="5.0">
<children>
<Button fx:id="addButton" contentDisplay="RIGHT" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="33.0" prefWidth="59.0" text="Add" HBox.hgrow="ALWAYS">
<graphic>
<ImageView fitHeight="14.0" fitWidth="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/add.png" />
</image>
<viewport>
<Rectangle2D height="14.0" width="14.0" />
</viewport>
</ImageView>
</graphic>
</Button>
<Button fx:id="editButton" contentDisplay="RIGHT" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="33.0" prefWidth="54.0" text="Edit" HBox.hgrow="ALWAYS">
<graphic>
<ImageView fitHeight="14.0" fitWidth="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/edit.png" />
</image>
<viewport>
<Rectangle2D height="14.0" width="14.0" />
</viewport>
</ImageView>
</graphic>
</Button>
<Button fx:id="deleteButton" contentDisplay="RIGHT" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="33.0" prefWidth="73.0" text="Delete" HBox.hgrow="ALWAYS">
<graphic>
<ImageView fitHeight="14.0" fitWidth="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/remove.png" />
</image>
<viewport>
<Rectangle2D height="14.0" width="14.0" />
</viewport>
</ImageView>
</graphic>
</Button>
</children>
</HBox>
<Separator maxHeight="0.0" minHeight="0.0" prefHeight="0.0" prefWidth="177.0" />
<ListView fx:id="itemListView" prefHeight="397.0" prefWidth="216.0" VBox.vgrow="ALWAYS" />
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding></VBox>
</children>
</StackPane>
样本单元格的FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.text.Font?>
<VBox fx:id="cellRootNode" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="107.0" prefWidth="255.0" spacing="5.0" style="-fx-background-color: #191919;" stylesheets="@MainWindowCSS.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="name" maxWidth="1.7976931348623157E308" text="ColorSwatch Name" textFill="WHITE" VBox.vgrow="ALWAYS" />
<Label fx:id="tagLine" maxWidth="1.7976931348623157E308" text="TagLine" textFill="#b3b3b3">
<graphic>
<ImageView fitHeight="21.0" fitWidth="21.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/tag.png" />
</image>
</ImageView>
</graphic>
<font>
<Font size="12.0" />
</font>
</Label>
<HBox prefHeight="12.0" prefWidth="64.0" spacing="5.0" VBox.vgrow="NEVER">
<children>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground" HBox.hgrow="NEVER">
<children>
<Circle fx:id="color1" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground">
<children>
<Circle fx:id="color2" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground">
<children>
<Circle fx:id="color3" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground">
<children>
<Circle fx:id="color4" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground">
<children>
<Circle fx:id="color5" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
</children>
</HBox>
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</VBox>
我在我的一个项目中遇到了同样的问题,我通过在fxml文件中提供fx:controller=
来修复它。您已经在主fxml中指定了,也可以使用fx:controller=“SwatchCell”
//在样例fxml文件中设置控制器。无论您的类包是什么添加您的fxml
。如果fxml添加到原始姿势,则我无法运行您的程序。这不是MCVE。我只是看看教程。我看不到哪里cellRootNode=fxmloader.load()代码>在教程中完成。本教程似乎做了fxmloader.load()
,仅此而已。您是如何提出您的产品线的?如前所述:请提供一个演示问题的示例;)是的,这是问题的一部分。我最终通过设置fx:controller使它工作起来,并且在单元格是否为空的情况下也出现了问题,我错过了bc。突然,我的NetBeans拒绝正确地自动格式化(缩进)我的代码。谢谢你的回答
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.text.Font?>
<VBox fx:id="cellRootNode" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="107.0" prefWidth="255.0" spacing="5.0" style="-fx-background-color: #191919;" stylesheets="@MainWindowCSS.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="name" maxWidth="1.7976931348623157E308" text="ColorSwatch Name" textFill="WHITE" VBox.vgrow="ALWAYS" />
<Label fx:id="tagLine" maxWidth="1.7976931348623157E308" text="TagLine" textFill="#b3b3b3">
<graphic>
<ImageView fitHeight="21.0" fitWidth="21.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/tag.png" />
</image>
</ImageView>
</graphic>
<font>
<Font size="12.0" />
</font>
</Label>
<HBox prefHeight="12.0" prefWidth="64.0" spacing="5.0" VBox.vgrow="NEVER">
<children>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground" HBox.hgrow="NEVER">
<children>
<Circle fx:id="color1" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground">
<children>
<Circle fx:id="color2" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground">
<children>
<Circle fx:id="color3" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground">
<children>
<Circle fx:id="color4" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
<StackPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="44.0" prefWidth="44.0" styleClass="swatchBackground">
<children>
<Circle fx:id="color5" fill="DODGERBLUE" radius="15.0" stroke="#ffffff00" strokeType="INSIDE" />
</children>
</StackPane>
</children>
</HBox>
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</VBox>