JavaFX表 - 如何添加组件? [英] JavaFX table- how to add components?

查看:154
本文介绍了JavaFX表 - 如何添加组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个swing项目,它使用许多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;

公共类GestureEvents扩展Application {

private TableView< Person> table = new TableView< Person>();
私人最终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),
新人(Ethan,Williams,ethan.williams @ example.com,The Button ),
新人(艾玛,琼斯,emma.jones @ example.com,按钮),
新人(迈克尔,布朗, michael.brown@example.com,按钮)

);

public static void main(String [] args){
launch(args);
}

@Override
public void start(舞台舞台){
场景场景=新场景(新组());
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(场景);
stage.show();
}

public static class Person {

private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
私人最终的SimpleStringProperty电子邮件;
私人决赛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 {
私人决赛JButton btn;
private ButtonPerson(){
btn = new JButton(The Button);
}
public JButton getButton(){
return btn;
}
}
}

编辑:经过进一步调查后我已找到使用预定义单元格类型(如文本和检查)覆盖单元格图形的示例。目前尚不清楚是否可以将任何通用jfx组件放置在像JFXPanel这样的单元格中。这与JTable不同,因为使用JTable我可以放置任何从JComponent继承的东西,只要我正确设置渲染类。如果有人知道如何(或者甚至可能)将JFXPanel放在单元格或其他通用JFx组件(如Button)中,这将非常有用。

解决方案

您的实施问题



您可以在Swing应用程序中嵌入JavaFX组件(通过将JavaFX组件放入用于在Swing中嵌入JavaFX组件而不是JavaFX中的Swing组件,因此它绝对会尝试在JavaFX TableView 中放置 JFXPanel 是没有意义的。这就是为什么你找不到任何人尝试这样的事情的例子。


这与JTable不同,因为使用JTable我可以放置任何东西只要我正确设置了渲染类,就从JComponent继承。


JavaFX TableView 类似于Swing JTable 就此而言。而不是 JComponent 节点是JavaFX的基本构建块 - 它们类似,但不同。只要为其提供适当的单元工厂,就可以在JavaFX TableView 中呈现任何节点



Java 8 中,有 SwingNode ,可以包含Swing JComponent SwingNode 允许您在JavaFX TableView中呈现任何Swing组件。



使用<$ c的代码$ c> SwingNode 非常简单:

  import javafx.application.Application; 
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import javax.swing。*;

公共类SwingFx扩展Application {
@Override public void start(Stage stage){
SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(() - > swingNode.setContent(new JButton(Click me!)));

stage.setScene(new Scene(new StackPane(swingNode),100,50));
stage.show();
}

public static void main(String [] args){launch(SwingFx.class); }
}

替代实施


基本上我要做的是为我的java项目添加手势滚动支持,这样用户就可以轻弹表格和标签


虽然Java 8应该允许你完全达到你想要的效果,但如果你更喜欢使用旧的Java版本,你可以使用基于Swing的用于Java的多点触控(MT4J)系统代替JavaFX。


I have a swing project that uses many JTables to display all sorts of things from text to panels to a mix of buttons and check boxes. I was able to do this by overwriting the table cell renderer to return generic JComponents. My question is can a similar table be made using JavaFx?

I want to update all my tables in the project to use JavaFx to support gestures mostly. It seems that TableView is the JavaFx component to use and I tried adding buttons to it but when displayed it shows the string value of the button, not the button itself. It looks like I have to overwrite the row factory or cell factory to do what I want but there are not a lot of examples. Here is the code I used as an example that displays the button as a string.

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;
        }
    }
}

Edit: After investigating further I've found examples that override cell graphics using predefined cell types like text and checks. It is not clear if any generic jfx component can be placed in a cell like a JFXPanel. This is unlike JTable, since using a JTable I can place anything that inherits from JComponent as long as I setup the render class correctly. If someone knows how (or if it's even possible) to place a JFXPanel in a cell or a other generic JFx component like a Button that would be very helpful.

解决方案

Issues with your Implementation

You can embed JavaFX components in Swing applications (by placing the JavaFX component in a JFXPanel). But you can't embed a Swing component in JavaFX (unless you are using JavaFX 8+).

JavaFX has it's own button implementation anyway, so there is no reason to embed a javax.swing.JButton on a JavaFX scene, even if you were using Java8 and it would work.

But that won't fix all of your issues. You are providing a cell value factory for your button column to supply buttons, but not supplying a custom cell rendering factory. The default table cell rendering factory renders the toString output on the respective cell value, which is why you just see the to string representation of the button in your table implementation.

You are putting buttons in your Person object. Don't do that - they don't belong there. Instead, dynamically generate a button in the cell rendering factory. This allows you to take advantage of a table's virtual flow technology whereby it only creates visual nodes for what you can see on the screen, not for every element in the table's backing data store. For example, if there are 10 rows visible on the screen and 10000 elements in the table, only 10 buttons will be created rather than 10000.

How to fix it

  1. Use JavaFX Button instead instead of javax.swing.JButton.
  2. Provide a cell rendering factory for your button.
  3. Generate a button in the table cell rather than in the Person.
  4. To set the button (or any arbitrary JavaFX node) in the table cell, use the cell's setGraphic method.

Correct Sample Code

This code incorporates the suggested fixes and a couple of improvements.

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.ObservableValue;
import javafx.collections.*;
import javafx.event.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

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","Coffee"),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com","Fruit"),
            new Person("Ethan", "Williams", "ethan.williams@example.com","Fruit"),
            new Person("Emma", "Jones", "emma.jones@example.com","Coffee"),
            new Person("Michael", "Brown", "michael.brown@example.com","Fruit")

        );

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        stage.setTitle("Table View Sample");

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        final Label actionTaken = new Label();

        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<Person, Person> btnCol = new TableColumn<>("Gifts");
        btnCol.setMinWidth(150);
        btnCol.setCellValueFactory(new Callback<CellDataFeatures<Person, Person>, ObservableValue<Person>>() {
          @Override public ObservableValue<Person> call(CellDataFeatures<Person, Person> features) {
              return new ReadOnlyObjectWrapper(features.getValue());
          }
        });
        btnCol.setComparator(new Comparator<Person>() {
          @Override public int compare(Person p1, Person p2) {
            return p1.getLikes().compareTo(p2.getLikes());
          }
        });
        btnCol.setCellFactory(new Callback<TableColumn<Person, Person>, TableCell<Person, Person>>() {
          @Override public TableCell<Person, Person> call(TableColumn<Person, Person> btnCol) {
            return new TableCell<Person, Person>() {
              final ImageView buttonGraphic = new ImageView();
              final Button button = new Button(); {
                button.setGraphic(buttonGraphic);
                button.setMinWidth(130);
              }
              @Override public void updateItem(final Person person, boolean empty) {
                super.updateItem(person, empty);
                if (person != null) {
                  switch (person.getLikes().toLowerCase()) {
                    case "fruit": 
                      button.setText("Buy fruit");
                      buttonGraphic.setImage(fruitImage);
                      break;

                    default:
                      button.setText("Buy coffee");
                      buttonGraphic.setImage(coffeeImage);
                      break;
                  }

                  setGraphic(button);
                  button.setOnAction(new EventHandler<ActionEvent>() {
                    @Override public void handle(ActionEvent event) {
                      actionTaken.setText("Bought " + person.getLikes().toLowerCase() + " for: " + person.getFirstName() + " " + person.getLastName());
                    }
                  });
                } else {
                  setGraphic(null);
                }
              }
            };
          }
        });

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

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 10, 10, 10));
        vbox.getChildren().addAll(label, table, actionTaken);
        VBox.setVgrow(table, Priority.ALWAYS);

        stage.setScene(new Scene(vbox));
        stage.show();
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;
        private final SimpleStringProperty likes;

        private Person(String fName, String lName, String email, String likes) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
            this.likes = new SimpleStringProperty(likes);
        }

        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 String getLikes() {
            return likes.get();
        }

        public void setLikes(String likes) {
            this.likes.set(likes);
        }
    }

    // icons for non-commercial use with attribution from: http://www.iconarchive.com/show/veggies-icons-by-iconicon/bananas-icon.html and http://www.iconarchive.com/show/collection-icons-by-archigraphs.html
    private final Image coffeeImage = new Image(
      "http://icons.iconarchive.com/icons/archigraphs/collection/48/Coffee-icon.png"
    );
    private final Image fruitImage = new Image(
      "http://icons.iconarchive.com/icons/iconicon/veggies/48/bananas-icon.png"
    );
}

On Using Swing Components in JavaFX

After investigating further I've found examples that override cell graphics using predefined cell types like text and checks. It is not clear if any generic jfx component can be placed in a cell like a JFXPanel.

A JFXPanel is for embedding a JavaFX component in Swing not a Swing component in JavaFX, hence it would make absolutely no sense to try to place a JFXPanel in a JavaFX TableView. This is why you find no examples of anybody attempting such a thing.

This is unlike JTable, since using a JTable I can place anything that inherits from JComponent as long as I setup the render class correctly.

A JavaFX TableView is similar to a Swing JTable in this regard. Instead of a JComponent, a Node is the basic building block for JavaFX - they are analogous, though different. You can render any Node in a JavaFX TableView as long as you supply the appropriate cell factory for it.

In Java 8, there is a SwingNode that can contain a Swing JComponent. A SwingNode allows you to render any Swing component inside a JavaFX TableView.

The code to use a SwingNode is very simple:

import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import javax.swing.*;

public class SwingFx extends Application {
  @Override public void start(Stage stage) {
    SwingNode swingNode = new SwingNode();
    SwingUtilities.invokeLater(() -> swingNode.setContent(new JButton("Click me!")));

    stage.setScene(new Scene(new StackPane(swingNode), 100, 50));
    stage.show();
  }

  public static void main(String[] args) { launch(SwingFx.class); }
}

Alternative Implementation

Basically what I'm trying to do is add gesture scrolling support to my java project so the user can 'flick' through the tables and tabs

Though Java 8 should allow you to achieve exactly what you want, if you prefer to use an older Java version, you could use the Swing based Multitouch for Java (MT4J) system for this instead of JavaFX.

这篇关于JavaFX表 - 如何添加组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆