我在使用javafx選項卡時遇到了一些問題。我需要在新表上獲取附加對象,即BillingTable類。我希望將它分配到字段表,但是我不斷收到一個異常,告訴我該轉換不起作用。選項卡存儲在帶名稱選項卡的tabPane中。這裏是我的代碼:將對象連接到javafx選項卡
tabs.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
@Override
public void changed(ObservableValue<? extends Tab> ov, Tab oldTab, Tab newTab) {
System.out.println("Tab Selection changed");
TreeTableView<BillingTable> treeTableView = (TreeTableView<BillingTable>) newTab.getContent();
table = treeTableView.getRoot().getValue();
}
});
BillingTable類:
public class BillingTable {
private TreeTableView<BillingTableRow> table;
private TreeItem<BillingTableRow> root;
private TreeTableColumn<BillingTableRow, String> nameColumn;
private TreeTableColumn<BillingTableRow, Double> totalColumn;
private TreeTableColumn<BillingTableRow, Double> dayColumn;
private TreeTableColumn<BillingTableRow, Double> eveningColumn;
private TreeTableColumn<BillingTableRow, Double> nightColumn;
private TreeTableColumn<BillingTableRow, Double> weekendColumn;
private TreeTableColumn<BillingTableRow, Double> holidayColumn;
private TreeTableColumn<BillingTableRow, Boolean> billedColumn;
/**
* Create a new billing table.
*/
public BillingTable() {
initiateTable();
}
/**
* Add row to the billing table.
*
* @return Added row.
*/
public TreeItem<BillingTableRow> addRow(BillingTableRow row) {
TreeItem<BillingTableRow> child = new TreeItem<>(row);
root.getChildren().add(child);
return child;
}
/**
* Add TreeItem to the table root.
*
* @param treeItem
* TreeItem to add to the root.
* @return Added row.
*/
public TreeItem<BillingTableRow> addRow(TreeItem<BillingTableRow> treeItem) {
root.getChildren().add(treeItem);
root.getValue().addChild(treeItem.getValue());
return treeItem;
}
/**
* Add client to the billing table.
*
* @param client
* Client to add.
* @return Created row in billing table.
*/
public TreeItem<BillingTableRow> addClient(Client client) {
TreeItem<BillingTableRow> row = new TreeItem<>(new BillingTableRow(client));
addRow(row);
return row;
}
/**
* Initiate TreeTableView of billing data.
*/
private void initiateTable() {
table = new TreeTableView<>();
// Define columns
nameColumn = new TreeTableColumn<>("Namn");
totalColumn = new TreeTableColumn<>("Summa");
dayColumn = new TreeTableColumn<>("Dag");
eveningColumn = new TreeTableColumn<>("Kväll");
nightColumn = new TreeTableColumn<>("Natt");
weekendColumn = new TreeTableColumn<>("Helg");
holidayColumn = new TreeTableColumn<>("Storhelg");
billedColumn = new TreeTableColumn<>("Faktureras");
// Change column sizing
nameColumn.setPrefWidth(150);
totalColumn.setPrefWidth(60);
dayColumn.setPrefWidth(60);
eveningColumn.setPrefWidth(60);
nightColumn.setPrefWidth(60);
weekendColumn.setPrefWidth(60);
holidayColumn.setPrefWidth(60);
billedColumn.setPrefWidth(80);
// Bind columns to variables
nameColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));
totalColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("timeTotal"));
dayColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("timeDay"));
eveningColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("timeEvening"));
nightColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("timeNight"));
weekendColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("timeWeekend"));
holidayColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("timeHoliday"));
billedColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("billed"));
// Make cells editable for all columns
makeEditableCells();
// Add columns to table
table.getColumns().add(nameColumn);
table.getColumns().add(totalColumn);
table.getColumns().add(dayColumn);
table.getColumns().add(eveningColumn);
table.getColumns().add(nightColumn);
table.getColumns().add(weekendColumn);
table.getColumns().add(holidayColumn);
table.getColumns().add(billedColumn);
root = new TreeItem<BillingTableRow>(new BillingTableRow("Root"));
root.setExpanded(true);
table.setRoot(root);
table.setShowRoot(false);
table.setTableMenuButtonVisible(true);
}
...
和異常:
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: gui.BillingTableRow cannot be cast to gui.BillingTable
at application.Main$1.changed(Main.java:136)
at application.Main$1.changed(Main.java:1)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(Unknown Source)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(Unknown Source)
at javafx.beans.property.ObjectPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.ObjectPropertyBase.set(Unknown Source)
at javafx.scene.control.SelectionModel.setSelectedItem(Unknown Source)
at javafx.scene.control.TabPane$TabPaneSelectionModel.select(Unknown Source)
at javafx.scene.control.TabPane$TabPaneSelectionModel.select(Unknown Source)
at javafx.scene.control.TabPane$TabPaneSelectionModel.findNearestAvailableTab(Unknown Source)
at javafx.scene.control.TabPane$TabPaneSelectionModel.lambda$new$17(Unknown Source)
at javafx.scene.control.TabPane$TabPaneSelectionModel$$Lambda$66/1951002621.onChanged(Unknown Source)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(Unknown Source)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(Unknown Source)
at javafx.collections.ObservableListBase.fireChange(Unknown Source)
at javafx.collections.ListChangeBuilder.commit(Unknown Source)
at javafx.collections.ListChangeBuilder.endChange(Unknown Source)
at javafx.collections.ObservableListBase.endChange(Unknown Source)
at javafx.collections.ModifiableObservableListBase.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at application.Main.addTab(Main.java:705)
at application.Main.start(Main.java:142)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$69/1795971577.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/1051754451.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1600778379.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
顯示您定義和添加選項卡的代碼。顯示「表」變量的定義。 – 2014-11-06 15:43:38
另外我想說明你在哪裏定義了「TreeTableView」及其根元素......我認爲根元素的值不是正確的運行時類型。如果你在任何地方有任何編譯器警告(特別是關於原始類型),那是第一個看的地方。 – 2014-11-06 15:57:25
請顯示證明問題的SSCCE。不這樣做需要浪費猜測(正如你在答案中看到的那樣;-)直到添加示例爲止的downvoting。 – kleopatra 2014-11-07 10:32:31