无法将javafx.scene.control.TableColumn强制转换为javafx.scene.control.TableColumn$CellDataFeatures
在填充javafx表视图时,我遇到了一个问题 我目前正在开发一个基于GUI的事件管理工具(用于大学),我一直在尝试填充一个Tableview列表,该列表应该位于边框窗格布局的中心 这是我的代码(它的思想相当长,它是主窗口函数):无法将javafx.scene.control.TableColumn强制转换为javafx.scene.control.TableColumn$CellDataFeatures,java,uitableview,javafx,tableview,tablecolumn,Java,Uitableview,Javafx,Tableview,Tablecolumn,在填充javafx表视图时,我遇到了一个问题 我目前正在开发一个基于GUI的事件管理工具(用于大学),我一直在尝试填充一个Tableview列表,该列表应该位于边框窗格布局的中心 这是我的代码(它的思想相当长,它是主窗口函数): import java.sql.Date; 导入java.sql.SQLException; 导入java.util.List; //--主要进口--// 导入javafx.application.application; 导入javafx.beans.property
import java.sql.Date;
导入java.sql.SQLException;
导入java.util.List;
//--主要进口--//
导入javafx.application.application;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.beans.property.SimpleObject属性;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
//--表格导入--//
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.control.TableColumn;
导入javafx.scene.text.Font;
导入javafx.stage.stage;
公共类主窗口扩展应用程序{
私有静态标签;
私有静态TableView表;
私有静态表列dateCol;
私有静态表列名称col;
专用静态表guestscol列号;
私有静态表列位置col;
私有静态边界窗格布局;
//这只是一些测试数据,我想这就是程序崩溃的地方
最终静态ObservableList数据=FXCollections.observableArrayList(
新事件(“雅各布”,“史密斯”,30,新日期(10000))
);
//----------------------//
//--私有常量--//
私有静态最终字符串DATE=“DATE”;
私有静态最终字符串NAME=“NAME”;
私有静态最终字符串NUMBER\u OF_GUESTS=“NUMBER OF GUESTS”;
私有静态最终字符串LOCATION=“LOCATION”;
//---------------------//
/**主要方法是启动应用程序*/
公共静态void main(字符串[]args){
试一试{
//--使用给定参数启动应用程序--//
启动(args);}
catch(异常e){e.printStackTrace();}
}
/**
*start方法,在使用“launch”时首先调用该方法
*/
@凌驾
public void start(Stage primaryWindow)引发异常{
//--初始化窗口中的所有对象--//
初始化对象();
//--获取布局--//
initializeLayout();
//--为窗口中的对象提供功能--//
功能化对象();
//--创建新场景--//
场景主视图=新场景(布局);
//--给主窗口一个标题并将其显示在前面--//
primaryWindow.setMinHeight(默认值);
设置最小宽度(默认宽度);
primaryWindow.setScene(主);
primaryWindow.show();
//TODO:在窗口关闭时,将列表内容保存到数据库
primaryWindow.setOnCloseRequest(null);
}
/**
*通过创建和命名主窗口的所有给定元素来初始化它们
*/
私有静态void初始化对象(){
//--桌子--//
tableHeadline=新标签(TABLE_HEAD);
tableHeadline.setFont(新字体(“Arial”,20));
table=新的TableView();
table.setEditable(true);
//--日期栏--//
dateCol=新表列(日期);
dateCol.setCellFactory(
新财产价值工厂(“日期”)
);
//--名称列--//
nameCol=新表列(名称);
nameCol.setCellValueFactory(
新PropertyValue工厂(“名称”)
);
//--宾客人数栏--//
numberOfGuestsCol=新的表格列(客人数量);
GuestScol.setMinWidth的数量(150);
numberOfGuestsCol.setCellValueFactory(
新酒店价值工厂(“客人数量”)
);
//--位置栏--//
locationCol=新表列(位置);
位置Col.setCellValueFactory(
新物业价值工厂(“位置”)
);
表2.设置项目(数据);
table.getColumns().addAll(nameCol、locationCol、,
Guestscol、dateCol的编号);
//TODO使用数据库中的数据填充
//--端台--//
}
/**
*为主窗口的元素指定功能
*/
私有静态对象(){
//--新…菜单项--//
新建设置操作(e->{
createReturn=CreatePopup.display();
if(createReturn){
//--获取用户输入--//
List toCreate=CreatePopup.getInput();
//--根据用户对列表的输入添加新事件--//
//这是应该插入新事件的地方,但我无法测试这一点,因为我没有走那么远
最终ObservableListdata=FXCollections.ObservableArrayList(
新事件(创建(0)、创建(1)、创建(2)、创建(3))
);
}
}
});
});
}
/**
*初始化主窗口的布局
*/
私有静态void initializeLayout(){
//--创建新布局--//
布局=新建边框窗格();
布局。机顶盒(菜单栏);
布局。设置中心(表);
}
公共静态类事件{
私有最终SimpleStringProperty名称;
私人物业的最终位置;
私人最终SimpleIntegerProperty numberOfGuests;
私有最终SimpleObject属性日期;
私有事件(字符串名称、字符串位置、整数、日期){
this.name=新的SimpleStringProperty(名称);
this.location=新的SimpleStringProperty(位置);
this.numberOfGuests=新的SimpleIntegerProperty(numOfGuests);
this.date=新的SimpleObject属性(日期);
}
//--------//
//吸气剂//
//--------//
公共SimpleStringProperty getName(){
返回名称;
}
公共SimpleStringProperty getLocation(){
返回位置;
}
公共SimpleIntegerProperty getNumberOfGuests(){
返回人数;
}
公共SimpleObject属性getDate(){
返回日期;
}
}
}
此代码将提供
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;
//--main imports--//
import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Label;
//--table imports--//
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.TableColumn;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class MainWindow extends Application {
private static Label tableHeadline;
private static TableView table;
private static TableColumn dateCol;
private static TableColumn nameCol;
private static TableColumn numberOfGuestsCol;
private static TableColumn locationCol;
private static BorderPane layout;
//This is just some test data, this is where the program crashes, I think
final static ObservableList<Event> data = FXCollections.observableArrayList(
new Event("Jacob", "Smith", 30, new Date(10000))
);
//----------------------//
//--Private constants--//
private static final String DATE = "Date";
private static final String NAME = "Name";
private static final String NUMBER_OF_GUESTS = "Number of guests";
private static final String LOCATION = "Location";
//---------------------//
/**The main method, launching the application*/
public static void main(String[] args) {
try {
//--launching the application with given arguments--//
launch(args);}
catch (Exception e) {e.printStackTrace();}
}
/**
* The start-method, that is called first when 'launch' is used
*/
@Override
public void start(Stage primaryWindow) throws Exception {
//--initialize all the objects in the window--//
initializeObjects();
//--get the layout--//
initializeLayout();
//--give functionality to the objects in the window--//
functionalizeObjects();
//--create the new scene--//
Scene main = new Scene(layout);
//--Giving the main window a title and bringing it up front--//
primaryWindow.setMinHeight(DEFAULT_WIN_HEIGHT);
primaryWindow.setMinWidth(DEFAULT_WIN_WIDTH);
primaryWindow.setScene(main);
primaryWindow.show();
//TODO: on window close, save the content of the list to the db
primaryWindow.setOnCloseRequest(null);
}
/**
* Initializes all given elements of the main window by creating and naming them
*/
private static void initializeObjects () {
//--table--//
tableHeadline = new Label (TABLE_HEAD);
tableHeadline.setFont(new Font ("Arial", 20));
table = new TableView<>();
table.setEditable(true);
//--date column--//
dateCol = new TableColumn<Event, Date>(DATE);
dateCol.setCellFactory(
new PropertyValueFactory<>("Date")
);
//--name column--//
nameCol = new TableColumn<Event, String>(NAME);
nameCol.setCellValueFactory(
new PropertyValueFactory<>("name")
);
//--numberOfGuests column--//
numberOfGuestsCol = new TableColumn<Event, Integer>(NUMBER_OF_GUESTS);
numberOfGuestsCol.setMinWidth(150);
numberOfGuestsCol.setCellValueFactory(
new PropertyValueFactory<>("Number of Guests")
);
//--location column--//
locationCol = new TableColumn<Event, String>(LOCATION);
locationCol.setCellValueFactory(
new PropertyValueFactory<>("location")
);
table.setItems(data);
table.getColumns().addAll(nameCol,locationCol,
numberOfGuestsCol,dateCol);
//TODO fill with data from the database
//--end table--//
}
/**
* Assign functionality to the elements of the main window
*/
private static void functionalizeObjects () {
//--new... MenuItem--//
newMItem.setOnAction(e -> {
createReturn = CreatePopup.display();
if (createReturn) {
//--get the user input--//
List<Object> toCreate = CreatePopup.getInput();
//--add a new event according to the user input to the list--//
//This is where new Events should be inserted, but I cannot test this as I do not come that far
final ObservableList<Event>data = FXCollections.ObservableArrayList(
new Event(toCreate(0),toCreate(1),toCreate(2),toCreate(3))
);
}
}
});
});
}
/**
* Initializes the layout of the main window
*/
private static void initializeLayout() {
//--Create a new layout--//
layout = new BorderPane();
layout.setTop(menuBar);
layout.setCenter(table);
}
public static class Event {
private final SimpleStringProperty name;
private final SimpleStringProperty location;
private final SimpleIntegerProperty numberOfGuests;
private final SimpleObjectProperty<Date> date;
private Event(String name, String location, int numOfGuests, Date date) {
this.name = new SimpleStringProperty(name);
this.location = new SimpleStringProperty(location);
this.numberOfGuests = new SimpleIntegerProperty(numOfGuests);
this.date = new SimpleObjectProperty<Date>(date);
}
//--------//
// Getter //
//--------//
public SimpleStringProperty getName() {
return name;
}
public SimpleStringProperty getLocation() {
return location;
}
public SimpleIntegerProperty getNumberOfGuests() {
return numberOfGuests;
}
public SimpleObjectProperty<Date> getDate() {
return date;
}
}
}
Exception in Application start method
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: javafx.scene.control.TableColumn cannot be cast to javafx.scene.control.TableColumn$CellDataFeatures
at javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:98)
at com.sun.javafx.scene.control.skin.TableRowSkin.getCell(TableRowSkin.java:87)
at com.sun.javafx.scene.control.skin.TableRowSkin.getCell(TableRowSkin.java:53)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.createCell(TableRowSkinBase.java:698)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.recreateCells(TableRowSkinBase.java:692)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:146)
at com.sun.javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:64)
at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)
at javafx.scene.control.Control.impl_processCSS(Control.java:859)
at javafx.scene.Parent.impl_processCSS(Parent.java:1272)
at javafx.scene.Parent.impl_processCSS(Parent.java:1272)
at javafx.scene.Parent.impl_processCSS(Parent.java:1272)
at javafx.scene.control.Control.impl_processCSS(Control.java:855)
at javafx.scene.Node.processCSS(Node.java:9056)
at javafx.scene.Node.processCSS(Node.java:9049)
at javafx.scene.Scene.doCSSPass(Scene.java:545)
at javafx.scene.Scene.access$3600(Scene.java:159)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2392)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$31(Toolkit.java:348)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:347)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:374)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$405(QuantumToolkit.java:319)
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.lambda$null$50(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)
dateCol.setCellFactory(...);
dateCol.setCellValueFactory(...);
TableView table ;
TableColumn dateCol ;
TableView<Event> table ;
TableColumn<Event, Date> dateCol ;
dateCol.setCellValueFactory(cellData -> cellData.getValue().getDate());
module foo.app {
opens com.foo; <-- place for with your class
}