gpt4 book ai didi

javafx tableview 未从 fxml 加载(为 null)

转载 作者:行者123 更新时间:2023-12-02 00:10:24 25 4
gpt4 key购买 nike

几个小时以来我一直在尝试查找错误。我不知道是什么原因导致这个问题。我正在使用 Controller 类和 fxml。关键是,初始化函数中的 TableView 为空。不知道为什么。这里是 Controller :

package controller.popup;

import controller.Controllerable;
import elements.EditableTableCell;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.WindowEvent;
import javafx.util.Callback;
import model.CDUSettings;
import model.Modelable;
import model.data.AnnouncementData;
import model.data.PopupData;
import model.popup.AnnouncmentsPopupModel;

public class AnnouncementsPopupController implements Controllerable, Initializable
{
// <editor-fold desc="fxml import stuff" defaultstate="collapsed">
@FXML
private TableView<PopupData> guiAnnouncementsTable;
@FXML // fx:id="guiAnnouncementAddColor"
private TextField guiAnnouncementAddColor; // Value injected by FXMLLoader
@FXML // fx:id="guiAnnouncementAddDisplayedText"
private TextField guiAnnouncementAddDisplayedText; // Value injected by FXMLLoader
@FXML // fx:id="guiAnnouncementAddDuration"
private TextField guiAnnouncementAddDuration; // Value injected by FXMLLoader
@FXML // fx:id="guiAnnouncementAddFontEffect"
private TextField guiAnnouncementAddFontEffect; // Value injected by FXMLLoader
@FXML // fx:id="guiAnnouncementAddFontSize"
private TextField guiAnnouncementAddFontSize; // Value injected by FXMLLoader
@FXML // fx:id="guiAnnouncementAddId"
private TextField guiAnnouncementAddId; // Value injected by FXMLLoader
@FXML // fx:id="guiAnnouncementAddType"
private TextField guiAnnouncementAddType; // Value injected by FXMLLoader
@FXML // fx:id="guiColorColumn"
private TableColumn<AnnouncementData, String> guiAnnouncementColorColumn; // Value injected by FXMLLoader
@FXML // fx:id="guiDisplayedTextColumn"
private TableColumn<AnnouncementData, String> guiAnnouncementDisplayedTextColumn; // Value injected by FXMLLoader
@FXML // fx:id="guiDurationColumn"
private TableColumn<AnnouncementData, String> guiAnnouncementDurationColumn; // Value injected by FXMLLoader
@FXML // fx:id="guiFontEffectColumn"
private TableColumn<AnnouncementData, String> guiAnnouncementFontEffectColumn; // Value injected by FXMLLoader
@FXML // fx:id="guiFontSizeColumn"
private TableColumn<AnnouncementData, String> guiAnnouncementFontSizeColumn; // Value injected by FXMLLoader
@FXML // fx:id="guiIdColumn"
private TableColumn<AnnouncementData, String> guiAnnouncementIdColumn; // Value injected by FXMLLoader
@FXML // fx:id="guiTypeColumn"
private TableColumn<AnnouncementData, String> guiAnnouncementTypeColumn; // Value injected by FXMLLoader
// </editor-fold>

private Modelable model = null;

public AnnouncementsPopupController(){
connectToModel();
}

@Override
public Modelable getModel(){
return model;
}

@Override
public void onCloseEvent(WindowEvent we)
{
CDUSettings.getInstance().clearAnnouncements();

for(PopupData announce : model.getData()){
CDUSettings.getInstance().addAnnouncement((AnnouncementData)announce);
}
}

@Override
public void initialize(URL url, ResourceBundle rb)
{
connectToModel();

Callback<TableColumn<AnnouncementData, String> ,TableCell<AnnouncementData, String>> editableFactory =
new Callback<TableColumn<AnnouncementData, String>, TableCell<AnnouncementData, String>>()
{
@Override
public TableCell call(TableColumn p){
return new EditableTableCell();
}
};

guiAnnouncementIdColumn.setCellValueFactory(new PropertyValueFactory<AnnouncementData, String>("id"));
guiAnnouncementIdColumn.setCellFactory(editableFactory);
guiAnnouncementIdColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AnnouncementData, String>>()
{
@Override
public void handle(TableColumn.CellEditEvent<AnnouncementData, String> t){
(t.getTableView().getItems().get(t.getTablePosition().getRow())).setId(t.getNewValue());
}
});

guiAnnouncementColorColumn.setCellValueFactory(new PropertyValueFactory<AnnouncementData, String>("color"));
guiAnnouncementColorColumn.setCellFactory(editableFactory);
guiAnnouncementColorColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AnnouncementData, String>>()
{
@Override
public void handle(TableColumn.CellEditEvent<AnnouncementData, String> t){
(t.getTableView().getItems().get(t.getTablePosition().getRow())).setColor(t.getNewValue());
}
});

guiAnnouncementDisplayedTextColumn.setCellValueFactory(new PropertyValueFactory<AnnouncementData, String>("displayedText"));
guiAnnouncementDisplayedTextColumn.setCellFactory(editableFactory);
guiAnnouncementDisplayedTextColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AnnouncementData, String>>()
{
@Override
public void handle(TableColumn.CellEditEvent<AnnouncementData, String> t){
(t.getTableView().getItems().get(t.getTablePosition().getRow())).setDisplayedText(t.getNewValue());
}
});

guiAnnouncementDurationColumn.setCellValueFactory(new PropertyValueFactory<AnnouncementData, String>("duration"));
guiAnnouncementDurationColumn.setCellFactory(editableFactory);
guiAnnouncementDurationColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AnnouncementData, String>>()
{
@Override
public void handle(TableColumn.CellEditEvent<AnnouncementData, String> t){
(t.getTableView().getItems().get(t.getTablePosition().getRow())).setDuration(t.getNewValue());
}
});

guiAnnouncementFontEffectColumn.setCellValueFactory(new PropertyValueFactory<AnnouncementData, String>("fontEffect"));
guiAnnouncementFontEffectColumn.setCellFactory(editableFactory);
guiAnnouncementFontEffectColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AnnouncementData, String>>()
{
@Override
public void handle(TableColumn.CellEditEvent<AnnouncementData, String> t){
(t.getTableView().getItems().get(t.getTablePosition().getRow())).setFontEffect(t.getNewValue());
}
});

guiAnnouncementFontSizeColumn.setCellValueFactory(new PropertyValueFactory<AnnouncementData, String>("fontSize"));
guiAnnouncementFontSizeColumn.setCellFactory(editableFactory);
guiAnnouncementFontSizeColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AnnouncementData, String>>()
{
@Override
public void handle(TableColumn.CellEditEvent<AnnouncementData, String> t){
(t.getTableView().getItems().get(t.getTablePosition().getRow())).setFontSize(t.getNewValue());
}
});

guiAnnouncementTypeColumn.setCellValueFactory(new PropertyValueFactory<AnnouncementData, String>("type"));
guiAnnouncementTypeColumn.setCellFactory(editableFactory);
guiAnnouncementTypeColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AnnouncementData, String>>()
{
@Override
public void handle(TableColumn.CellEditEvent<AnnouncementData, String> t){
(t.getTableView().getItems().get(t.getTablePosition().getRow())).setType(t.getNewValue());
}
});

guiAnnouncementsTable.setItems(model.getData());//guiAnnouncementsTable==null at this point
}

public void newAddBtnEvent(ActionEvent event)
{
model.addDataEntry(new AnnouncementData(guiAnnouncementAddDisplayedText.getText(),
guiAnnouncementAddId.getText(),
guiAnnouncementAddColor.getText(),
guiAnnouncementAddFontEffect.getText(),
guiAnnouncementAddFontSize.getText(),
guiAnnouncementAddType.getText(),
guiAnnouncementAddDuration.getText()));
guiAnnouncementAddColor.clear();
guiAnnouncementAddDisplayedText.clear();
guiAnnouncementAddDuration.clear();
guiAnnouncementAddFontEffect.clear();
guiAnnouncementAddFontSize.clear();
guiAnnouncementAddId.clear();
guiAnnouncementAddType.clear();

guiAnnouncementsTable.setItems(model.getData());
}

private void connectToModel()
{
if(model == null){
model = new AnnouncmentsPopupModel();
}
}
}

这里是 fxml:

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

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

<AnchorPane id="AnchorPane" minHeight="397.0" minWidth="264.0" prefHeight="397.0" prefWidth="819.0" xmlns:fx="http://javafx.com/fxml" fx:controller="controller.popup.AnnouncementsPopupController">
<children>
<Label text="Announcements" AnchorPane.leftAnchor="14.0" AnchorPane.topAnchor="6.0">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<TableView fx:id="guiAnnouncementTable" editable="true" prefHeight="311.0" prefWidth="236.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="52.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="34.0">
<columns>
<TableColumn minWidth="75.0" prefWidth="100.0" text="Displayed Text" fx:id="guiAnnouncementDisplayedTextColumn" />
<TableColumn prefWidth="75.0" text="ID" fx:id="guiAnnouncementIdColumn" />
<TableColumn prefWidth="75.0" text="Color" fx:id="guiAnnouncementColorColumn" />
<TableColumn prefWidth="75.0" text="FontEffect" fx:id="guiAnnouncementFontEffectColumn" />
<TableColumn prefWidth="75.0" text="FontSize" fx:id="guiAnnouncementFontSizeColumn" />
<TableColumn prefWidth="75.0" text="Type" fx:id="guiAnnouncementTypeColumn" />
<TableColumn prefWidth="75.0" text="Duration" fx:id="guiAnnouncementDurationColumn" />
</columns>
</TableView>
<HBox id="HBox" alignment="CENTER" prefWidth="236.0" spacing="5.0" AnchorPane.bottomAnchor="17.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0">
<children>
<TextField id="guiAnnouncementAddDisplayyedText" fx:id="guiAnnouncementAddDisplayedText" prefWidth="200.0" promptText="Displayed Text" />
<TextField fx:id="guiAnnouncementAddId" prefWidth="79.0" promptText="ID" />
<TextField fx:id="guiAnnouncementAddColor" prefWidth="79.0" promptText="Color" />
<TextField fx:id="guiAnnouncementAddFontEffect" prefWidth="79.0" promptText="Font Effect" />
<TextField fx:id="guiAnnouncementAddFontSize" prefWidth="79.0" promptText="Font Size" />
<TextField fx:id="guiAnnouncementAddType" prefWidth="79.0" promptText="Type" />
<TextField fx:id="guiAnnouncementAddDuration" prefWidth="79.0" promptText="Duration" />
<Button mnemonicParsing="false" onAction="#newAddBtnEvent" text="Add" />
</children>
</HBox>
</children>
</AnchorPane>

我很绝望;(

最佳答案

我猜你在代码中犯了拼写错误。在您的 java 文件中:

private TableView<PopupData> guiAnnouncement**s**Table;

变量名中有“s”。在您的 fxml 中:

   <TableView fx:id="guiAnnouncementTable"

没有s

关于javafx tableview 未从 fxml 加载(为 null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12937401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com