我已经按照this教程学习了如何自定义FXML中定义的结构的listview单元格。我像往常一样定义了cellFactory:
itemListView.setCellFactory((ListView studentListView)->新的SwatchCell());
具有如下定义的SwatchCell类:
EventData.SequenceNumber
FXML是使用scenebuilder生成的,并且在NetBeans代码中不会出现语法错误。为了您的想象,FXML看起来像这样: screen 。由于某种原因,程序最终在SwatchCell类的loadFXML方法的catch块中结束。这是stackTrace
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);
}
}
}
。我希望我包括所有相关代码。
老实说,我不完全了解CallBack和CellFactory如何详细工作,这是第一次将List Cell如此程度地改变。即使我知道我不应该问“为我解决这个问题”,对我来说最好的学习方法是通过学习工作示例。我显然希望使listviews的单元格采用FXML文件的外观。感谢您的帮助。
PS:所有FXML代码都是在Scene Builder中生成的,并且不会在NetBeans中弹出任何错误,因此从语法上讲应该没问题。 FX:ID应该正确分配。
mainWindow的FXML:
Aug 15, 2018 2:25:58 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.171 by JavaFX runtime of version 8.0.161
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at ui.SwatchCell.updateItem(SwatchCell.java:29)
at ui.SwatchCell.updateItem(SwatchCell.java:11)
at javafx.scene.control.ListCell.updateItem(ListCell.java:471)
at javafx.scene.control.ListCell.indexChanged(ListCell.java:330)
at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1957)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879)
at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene.preferredSize(Scene.java:1646)
at javafx.scene.Scene.impl_preferredSize(Scene.java:1720)
at javafx.stage.Window$9.invalidated(Window.java:864)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.stage.Window.setShowing(Window.java:940)
at javafx.stage.Window.show(Window.java:955)
at javafx.stage.Stage.show(Stage.java:259)
at ui.MainWindow.show(MainWindow.java:51)
at ui.launcher.start(launcher.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application ui.launcher
C:\Users\Jan\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
SwatchCell的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>
答案 0 :(得分:0)
我在一个项目中遇到了同样的问题,我通过在我的fxml文件中输入fx:controller=
来解决此问题。您已经在主fxml中给出了设置,也可以使用fx:controller="SwatchCell"
///无论您的类包是什么,在swatch fxml文件中设置控制器