2015-09-04 169 views
0

我在填写javafx tableview时遇到了问题。

我目前正在研究一个基于GUI的事件管理工具(适用于大学),而且我试图填充一个Tableview列表,该列表应该位于边框窗格布局的中心。

这是我的代码(它很早就想到了,它的主窗口功能):

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) 

我想不出任何理由这个奇怪的错误。我也搜索了很长时间,并尝试了很多东西,但它仍然在启动时出现。

现在,我指望你,社区,请帮助我!

预先感谢阅读长职位..

+0

请减少问题中的代码。大部分代码与您遇到的错误无关。 –

+0

就我认为相关的代码仍然在那里编辑 –

+0

您至少可以指出哪条线路正在产生错误? –

回答

12

你有

dateCol.setCellFactory(...); 

,而不是

dateCol.setCellValueFactory(...); 

这就是为什么你不应该使用原始类型的一个很好的例子你的代码,并应避免像PropertyValueFactory这样的API支持类型安全代码。

I.e.你有

TableView table ; 
TableColumn dateCol ; 

时,你应该有类似

TableView<Event> table ; 
TableColumn<Event, Date> dateCol ; 

然后你就可以做

dateCol.setCellValueFactory(cellData -> cellData.getValue().getDate()); 

如果编码是这样,编译器会马上拿起你的错误。

作为旁白(也许,另一边),你应该真的让你的Event类遵循正确的JavaFX属性模式,如tutorial中所述。如果你不这样做,你可能会看到意外的行为。

+0

我只是试图抵制敦促你问为什么在地球上,你做了一切'静态'... –

+0

非常感谢你,我让所有的东西都是静态的,以便区分初始化和功能化。这很糟糕吗? –

相关问题