我有一个摇摆项目,它使用许多 JTable 来显示从文本到面板到混合按钮和复选框的各种内容。我可以通过覆盖表格单元格渲染器以返回通用 JComponents 来做到这一点。我的问题是可以使用 JavaFx 制作类似的表格吗?
我想更新项目中的所有表以使用 JavaFx 来主要支持手势。似乎 TableView 是要使用的 JavaFx 组件,我尝试向它添加按钮,但是在显示时它显示的是按钮的字符串值,而不是按钮本身。看起来我必须覆盖行工厂或单元工厂才能做我想做的事,但例子并不多。这是我用作将按钮显示为字符串的示例的代码。
import javax.swing.JButton;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class GestureEvents extends Application {
private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com","The Button"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com","The Button"),
new Person("Ethan", "Williams", "ethan.williams@example.com","The Button"),
new Person("Emma", "Jones", "emma.jones@example.com","The Button"),
new Person("Michael", "Brown", "michael.brown@example.com","The Button")
);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));
TableColumn btnCol = new TableColumn("Buttons");
btnCol.setMinWidth(100);
btnCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("btn"));
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, btnCol);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private final JButton btn;
private Person(String fName, String lName, String email, String btn) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
this.btn = new JButton(btn);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
public JButton getBtn(){
return btn;
}
public void setBtn(String btn){
}
}
public static class ButtonPerson{
private final JButton btn;
private ButtonPerson(){
btn = new JButton("The Button");
}
public JButton getButton(){
return btn;
}
}
}
编辑:在进一步调查后,我发现使用预定义的单元格类型(如文本和检查)覆盖单元格图形的示例。尚不清楚是否可以将任何通用 jfx 组件放置在像 JFXPanel 这样的单元格中。这与 JTable 不同,因为使用 JTable,只要我正确设置了渲染类,我就可以放置从 JComponent 继承的任何东西。如果有人知道如何(或者甚至可能)将 JFXPanel 放置在单元格或其他通用 JFx 组件(如 Button)中,那将非常有帮助。