我正在尝试将TableView放在分页中,我为FXML修改了一个示例,一个小时就可以了,但是现在显示此

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = TableRow@4a034b[styleClass=cell indexed-cell table-row-cell]'null'
at javafx.scene.Parent$2.onProposedChange(Parent.java:458)
at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:115)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:507)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.checkState(TableRowSkinBase.java:593)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.computePrefHeight(TableRowSkinBase.java:528)
at javafx.scene.control.Control.computePrefHeight(Control.java:543)
at javafx.scene.Parent.prefHeight(Parent.java:924)
at javafx.scene.layout.Region.prefHeight(Region.java:1438)
at com.sun.javafx.scene.control.skin.VirtualFlow.resizeCellSize(VirtualFlow.java:1782)
at com.sun.javafx.scene.control.skin.VirtualFlow.addLeadingCells(VirtualFlow.java:1146)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1099)
at javafx.scene.Parent.layout(Parent.java:1076)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Scene.doLayoutPass(Scene.java:576)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2386)
at com.sun.javafx.tk.Toolkit$3.run(Toolkit.java:321)
at com.sun.javafx.tk.Toolkit$3.run(Toolkit.java:319)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:319)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:348)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:479)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:460)
at com.sun.javafx.tk.quantum.QuantumToolkit$13.run(QuantumToolkit.java:327)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.access$200(GtkApplication.java:48)
at com.sun.glass.ui.gtk.GtkApplication$6$1.run(GtkApplication.java:149)
at java.lang.Thread.run(Thread.java:745)


如果我将分页更改为2或3并返回1,则无法选择该行

这是我的代码

@FXML
private Pagination pageTask;

private final ObservableList<Agenda> tasksList = FXCollections.observableArrayList();

private final TableView<Agenda> table = new TableView<>();
private final TableColumn<Agenda, LocalDate> dateCol = new TableColumn("Data");
private final TableColumn<Agenda, String> localCol = new TableColumn("Local");
private final TableColumn<Agenda, String> descCol = new TableColumn("Descrição");
private final TableColumn<Agenda, Boolean> confirmCol = new TableColumn("Confirmado");
private final TableColumn<Agenda, String> userCol = new TableColumn("Usuario Participante");

public int itemsPerPage()
{
    return 1;
}

public int rowsPerPage()
{
    return 10;
}

public void createTable(int page, int lastIndex, int pageIndex, int displace, VBox box)
{

    for (int i = page; i < page + itemsPerPage(); i++)
    {
        dateCol.setCellValueFactory(
                new PropertyValueFactory<>("date"));

        dateCol.setMinWidth(20);

        localCol.setCellValueFactory(
                new PropertyValueFactory<>("local"));

        localCol.setMinWidth(140);

        descCol.setCellValueFactory(
                new PropertyValueFactory<>("desciption"));

        descCol.setMinWidth(140);

        userCol.setCellValueFactory(
                new PropertyValueFactory<>("nomeUser"));

        descCol.setMinWidth(140);

        confirmCol.setCellValueFactory(
                new PropertyValueFactory<>("confirm"));

        confirmCol.setMinWidth(20);

        table.getColumns().addAll(dateCol, localCol, userCol, descCol, confirmCol);
        table.setItems(tasksList);
        box.getChildren().add(table);
    }
}

public VBox createPage(int pageIndex)
{
    int lastIndex = 0;
    int displace = tasksList.size() % rowsPerPage();
    if (displace > 0)
        lastIndex = tasksList.size() / rowsPerPage();
    else
        lastIndex = tasksList.size() / rowsPerPage() - 1;

    VBox box = new VBox(5);
    int page = pageIndex * itemsPerPage();
    createTable(page, lastIndex, pageIndex, displace, box);
    return box;
}

public void setPage()
{
    pageTask.setPageCount((tasksList.size() / rowsPerPage() - 1));
    pageTask.setCurrentPageIndex(0);
    pageTask.setPageFactory(pageIndex ->
    {
        if (pageIndex > tasksList.size() / rowsPerPage() + 1)
            return null;
        else
            return createPage(pageIndex);
    });
}

@Override
public void initialize(URL url, ResourceBundle rb)
{
    populateTableAgenda();
    table.setOnMouseClicked(ms ->
    {
        if (table.getSelectionModel().getSelectedIndex() >= 0)
            selectItem();

    });

    setPage();
    dateTask.requestFocus();
    dateTask.getEditor().setOnMouseClicked(e ->
    {
        dateTask.show();
    });
}


和我的fxml

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

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="572.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.omnieyes.controller.AgendaController">
<children>
    <Pagination fx:id="pageTask" layoutX="22.0" layoutY="31.0" prefHeight="306.0" prefWidth="525.0" />
  <VBox layoutX="7.0" layoutY="2.0">
     <children>
          <HBox alignment="TOP_CENTER" layoutX="132.0" layoutY="2.0" spacing="40.0" VBox.vgrow="ALWAYS">
              <children>
              <Button fx:id="btnInsertTask" mnemonicParsing="false" onAction="#newTask" text="Inserir Nova Tarefa" HBox.hgrow="ALWAYS" />
                  <Label alignment="CENTER" layoutX="111.0" layoutY="6.0" text="Tarefas Do Dia" textAlignment="CENTER" wrapText="true" HBox.hgrow="ALWAYS" />

                <DatePicker fx:id="dateTask" editable="false" nodeOrientation="LEFT_TO_RIGHT" onAction="#filterPerDate" prefHeight="25.0" prefWidth="207.0" promptText="Pesquisar  por Data" />
              </children>
          </HBox>
     </children>
  </VBox>
    <Separator layoutX="8.0" layoutY="336.0" prefHeight="17.0" prefWidth="555.0" />
    <Label layoutX="213.0" layoutY="344.0" text="Dados  da Tarefa" />
    <HBox layoutX="14.0" layoutY="366.0" spacing="50.0">
        <children>
            <Label fx:id="date" layoutX="14.0" layoutY="370.0" text="Data" visible="false" />
        <HBox>
           <children>
                  <Label alignment="CENTER" layoutX="90.0" layoutY="375.0" text="Local" textAlignment="CENTER" translateY="5.0" HBox.hgrow="ALWAYS">
                      <HBox.margin>
                          <Insets />
                      </HBox.margin>
                  </Label>
                  <TextField fx:id="txtLocalTask" layoutX="125.0" layoutY="366.0" HBox.hgrow="ALWAYS">
                      <HBox.margin>
                          <Insets />
                      </HBox.margin>
                  </TextField>
           </children>
        </HBox>
            <CheckBox fx:id="confirmTask" layoutX="294.0" layoutY="370.0" mnemonicParsing="false" text="Confirmar Presença">
                <HBox.margin>
                    <Insets />
                </HBox.margin>
            </CheckBox>
        </children>
    </HBox>
    <HBox layoutX="13.0" layoutY="400.0" spacing="385.0">
        <children>
            <Button fx:id="saveTask" layoutX="13.0" layoutY="400.0" mnemonicParsing="false" onAction="#saveTask" text="Salvar" />
            <Button fx:id="removeTask" layoutX="418.0" layoutY="400.0" mnemonicParsing="false" onAction="#removeTask" text="Remover " />
        </children>
    </HBox>
</children>

最佳答案

我已经解决了这个问题。引用FXML文件时,应使用绝对路径。像这样 :

Parent root = FXMLLoader.load(getClass().getResource("/Hos/Hospital.fxml"));

关于java - 线程“JavaFX Application Thread”中的异常java.lang.IllegalArgumentException:子代:添加了重复的子代:parent = TableRow @ adc848,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23874795/

10-09 07:07