【问题标题】:Javafx table in SwingSwing 中的 Javafx 表
【发布时间】:2018-08-18 22:49:05
【问题描述】:

我有一个 Swing 应用程序。我想在我的项目中使用 JavaFx。我无法将 TableView 添加到 Jframe 的面板中。请帮我将 TableView 添加到 Jframe 的面板中。

我有一个 Swing 应用程序。我想在我的项目中使用 JavaFx。我无法将 TableView 添加到 Jframe 的面板中。请帮我将 TableView 添加到 Jframe 的面板中。

   TableView<Person> table = new TableView<Person>();
   final ObservableList<Person> data =
        FXCollections.observableArrayList(
        new Person("Jacob", "Smith", "jacob.smith@example.com"),
        new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
        new Person("Ethan", "Williams", "ethan.williams@example.com"),
        new Person("Emma", "Jones", "emma.jones@example.com"),
        new Person("Michael", "Brown", "michael.brown@example.com"));
    final HBox hb = new HBox();
    private Person(String fName, String lName, String email) {
        this.firstName = new SimpleStringProperty(fName);
        this.lastName = new SimpleStringProperty(lName);
        this.email = new SimpleStringProperty(email);
    }

    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 NewJFrame1() {
    initComponents();
    Scene scene = new Scene(new Group());


    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"));

    table.setItems(data);
    table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

    final TextField addFirstName = new TextField();
    addFirstName.setPromptText("First Name");
    addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
    final TextField addLastName = new TextField();
    addLastName.setMaxWidth(lastNameCol.getPrefWidth());
    addLastName.setPromptText("Last Name");
    final TextField addEmail = new TextField();
    addEmail.setMaxWidth(emailCol.getPrefWidth());
    addEmail.setPromptText("Email");

    final Button addButton = new Button("Add");
    addButton.setStyle("-fx-background-color: gray; -fx-text-fill: white;");
    addButton.getStyleClass().add("button1");
    addButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            data.add(new Person(
                    addFirstName.getText(),
                    addLastName.getText(),
                    addEmail.getText()));
            addFirstName.clear();
            addLastName.clear();
            addEmail.clear();
        }
    });

    hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);
    hb.setSpacing(3);

    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10, 0, 0, 10));
    vbox.getChildren().addAll(label, table, hb);

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    PopupMenu s;
    JFXPanel jf = new JFXPanel();
    jf.add(table);

    jPanel1.add(jf);





}

【问题讨论】:

  • 您看过 JavaFX 库中的 JFXPanel 类了吗?
  • 是的,但我不能。
  • 为什么不能使用 JFXPanel?
  • 请将您的代码 sn-ps 添加到您的问题中,我需要能够看到您尝试了什么。
  • 您的问题是不能直接将 TableView 添加到 JFXPanel,您必须先使用场景填充 JFXPanel,然后添加 TableView。

标签: java swing javafx tableview


【解决方案1】:

您不能将TableView 直接添加到JFXPanel。您必须首先使用Scene 填充JFXPanel,然后您可以将TableView 添加到Scene

干杯!

【讨论】:

    猜你喜欢
    • 2018-01-22
    • 2018-10-26
    • 1970-01-01
    • 2014-07-28
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2021-02-28
    相关资源
    最近更新 更多