- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在填充 javafx tableview 时遇到问题。
我目前正在开发基于 GUI 的事件管理工具(适用于大学),但我一直在尝试填充 Tableview 列表,该列表应该位于边框 Pane 布局的中心。
这是我的代码(它的想法很长,它的主窗口函数):
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(...);
PropertyValueFactory
这样的 API。支持类型安全代码。
TableView table ;
TableColumn dateCol ;
TableView<Event> table ;
TableColumn<Event, Date> dateCol ;
dateCol.setCellValueFactory(cellData -> cellData.getValue().getDate());
Event
类遵循正确的 JavaFX 属性模式,如
tutorial 中所述.如果不这样做,您可能会看到意外行为。
关于javafx.scene.control.TableColumn 不能转换为 javafx.scene.control.TableColumn$CellDataFeatures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32398007/
String[] titles = {"System Code","Domain Name","Organizational Unit","Organization Name"}; f
我在填充 javafx tableview 时遇到问题。 我目前正在开发基于 GUI 的事件管理工具(适用于大学),但我一直在尝试填充 Tableview 列表,该列表应该位于边框 Pane 布局的中
当我在 JavaFX 中生成表格时,列太大,我想在它出现在我的应用程序上时调整它们的大小以减少它们。我读了 stackOverflow 上的那些文章,这些文章很有用,但对我没有帮助:javafx co
我有一个账单表,我想在其中列出账单上的所有产品。我保存了ProductInBill ArrayList 内的对象在账单上。 当我创建TableView时我的常用方法是创建 JavaFX 字段。在 Co
我正在尝试使用 javaFX Scene builder 获取可编辑表的值,但无法获取更新的值。 我的表有 2 列:名称列和值列。每当用户更改 value 列的值时,我想使用react。 但是当我打印
我正在使用 JavaFX 制作表格。每行都有文字。一行有一个图形,因为该单元格的文本有多种颜色。 该代码仅在特定条件为真时才适用(该部分有效): departTimeCol.setCellFactor
我有一个包含三列的表,我想确保列的总宽度等于表的宽度,因此我使用 CONSTRAINED_RESIZE_POLICY。 我希望列以我指定的宽度开始,但用户可以修改。 如果我这样做: albumTabl
我正在制作一个使用表格 View 的程序。一切都很顺利,除了由于某种原因我无法将整数值填充到表中。下面是我在主程序中的代码,当我运行程序时,字符串和 double 会填充,但整数不会。在我的 Prod
我正在为我的 TableColumns 创建一个自定义标题,它是列的标签加上一个允许用户执行搜索的 TextField。我正在像这样设置列标题: getColumns().addListener(ne
我在调整包含环绕 TableCell 项目的文本项目的 TableView 大小时遇到问题。调整大小时,隐藏值会调整大小,但可见项不会重新计算文本换行。 红色框中的推文在调整大小时被隐藏,并按预期
要点 在 JavaFX TableColumn 中,右侧有一个排序箭头。 如何设置此箭头的对齐方式? 我的用例 我问是因为我正在尝试申请 Material Design到 JavaFX 并且箭头需要在
在 OpenJFX 11.0.2 & 12.0.1 SDK (Windows 10, x64) 中复制,不能在 JavaFX 8 中复制 右键单击表列,然后尝试调整列的大小。不会显示调整大小光标,并且
这是我的问题: 为 TableColumn 的默认排序设置自定义排序算法。 背景:作为示例列,我想使用将 IPv4 地址表示为 String 的列。因此默认排序是字典顺序(1.1.1.1、1.1.1.
有没有办法阻止用户调整TableView中的任何或至少所有JavaFX TableColumn的大小,同时仍然允许调整大小策略起作用? 存在 TableColumn::setResizing,但在阻止
我正在替换表列的 javafx 上下文菜单。上下文菜单将使用户能够访问 excel,例如表格 View 的过滤。一切正常,但我现在不想在列标题上的任何鼠标单击事件上打开上下文菜单。不仅仅是标准的右键单
以下场景:下表/列中有一些行具有长字符串值。我想出了如何通过手动调用“setCellFactory”并将“text.wrappingWidthProperty()”绑定(bind)到列的宽度来解决包装
我有追随者 http://code.makery.ch/library/javafx-8-tutorial/part2/并且在 TableColumn 中有一个使用 lambdas 的代码设置值: f
有人可以解释我的代码有什么问题吗? 我想创建一个简单的 swt 表。 Eclipse 注意到 TableColumn 是未定义的: TableColumn column = new Table
private float[] cwidth = { 0.1f, 0.3f, 0.3f, 0.2f, 0.1f }; setAutoResizeMode(JTable.AUTO_RESIZE_OFF)
我正在 Gluon Scene Builder 中构建一个 TableView。我想为每列中的单元格设置不同的背景颜色。我能否仅通过 FXML 文件和 CSS 来完成此操作? 使用 CSS 样式类 .
我是一名优秀的程序员,十分优秀!