列表单元中未设置位置

时间:2019-07-03 05:06:14

标签: java javafx

java.lang.IllegalStateException:未设置位置。尝试加载自定义单元时收到错误 我在此处找到的所有示例以及在网络上其他位置都没有提及我遇到的任何情况

ListCell.fxml

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>

<Pane id="CellPane" fx:id="CellPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="39.0" prefWidth="286.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Test.ListCellController">
   <children>
      <Label id="CellLabel" fx:id="CellLabel" alignment="CENTER" layoutX="3.0" layoutY="5.0" prefHeight="30.0" prefWidth="279.0" />
   </children>
</Pane>

ListCellController

public class ListCellController extends ListCell<Student> 
{
    FXMLLoader loader;
    @FXML
    private Label cellLabel;
    @FXML
    private Pane CellPane;

   @Override
   protected void updateItem(Student student, boolean empty)
   {
       super.updateItem(student, empty);

       if (empty || student == null)
       {
           setText(null);
           setGraphic(null);
       }
       else
       {
           if (loader == null)
           {
               loader = new FXMLLoader(getClass().getResource("listCell.fxml"));
               loader.setController(this);
           }

           try
           {
               loader.load(); //Debug tools show error starts here
           }
           catch (IOException ex)
           {
                ex.printStackTrace();
           }
           cellLabel.setText(student.getName() + " - " + student.getUserId() + " - " + student.getAttendence());

            setText(null);
            setGraphic(CellPane);
       }
   }
}

它应该正在将自定义单元格加载到列表视图中,相反,当它尝试运行loader.load()行时,它给出了“位置未设置”错误

我不能锻炼为什么它需要一个位置集,在这种情况下,我想它会从放置在列表视图中得到它

错误日志:

java.lang.IllegalStateException: Location is not set.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at Test.ListCellController.updateItem(ListCellController.java:51)
    at Test.ListCellController.updateItem(ListCellController.java:23)
    at javafx.controls/javafx.scene.control.ListCell.updateItem(ListCell.java:478)
    at javafx.controls/javafx.scene.control.ListCell.indexChanged(ListCell.java:337)
    at javafx.controls/javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:120)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1708)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1692)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1818)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2721)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1206)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
    at javafx.graphics/javafx.scene.Scene.doLayoutPass(Scene.java:576)
    at javafx.graphics/javafx.scene.Scene.preferredSize(Scene.java:1748)
    at javafx.graphics/javafx.scene.Scene$2.preferredSize(Scene.java:393)
    at javafx.graphics/com.sun.javafx.scene.SceneHelper.preferredSize(SceneHelper.java:66)
    at javafx.graphics/javafx.stage.Window$12.invalidated(Window.java:1086)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.graphics/javafx.stage.Window.setShowing(Window.java:1174)
    at javafx.graphics/javafx.stage.Window.show(Window.java:1189)
    at javafx.graphics/javafx.stage.Stage.show(Stage.java:273)
    at Test.ListViewTest.start(ListViewTest.java:40)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    at java.base/java.lang.Thread.run(Thread.java:835)

最好的问候

0 个答案:

没有答案