javafx tableview未从fxml加载(为空)
我试图从小时后发现错误。我不知道是什么原因造成这个问题。我正在使用一个控制器类和一个fxml。问题是,在初始化函数中tableview是null。不知道为什么。 这里的控制器:javafx tableview未从fxml加载(为空)
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"
没有小号
以上是 javafx tableview未从fxml加载(为空) 的全部内容, 来源链接: utcz.com/qa/257508.html