java JavaFX:如何刷新表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32027493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:31:17  来源:igfitidea点击:

JavaFX: How to refresh table?

javajavafx

提问by stdex

I have problem with refresh rows styles in JavaFX TableView.

我在 JavaFX TableView 中刷新行样式有问题。

java version "1.8.0_51"

Java(TM) SE Runtime Environment (build 1.8.0_51-b16)

Java HotSpot(TM) Server VM (build 25.51-b03, mixed mode)

java版本“1.8.0_51”

Java(TM) SE 运行时环境(构建 1.8.0_51-b16)

Java HotSpot(TM) 服务器 VM(构建 25.51-b03,混合模式)

Logic:

逻辑:

  1. Load data to tableView.
  2. Set new styles to rows by setRowFactory.
  3. Load new data to table.
  4. Refresh styles for new table rows. (Not working for me.)
  1. 将数据加载到 tableView。
  2. 通过 setRowFactory 为行设置新样式。
  3. 将新数据加载到表中。
  4. 刷新新表格行的样式。(不适合我。)

How to reload styles for rows?

如何重新加载行的样式?

My code snippet:

我的代码片段:

import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

public class RowFactoryAndOptimisationDemo extends Application {

        VBox root;

        public ObservableList<RFDDomain> data = FXCollections.observableArrayList();
        public TableView<RFDDomain> tableView = new TableView<RFDDomain>();

    @Override
    public void start(Stage stage) throws Exception {
                root = new VBox();
        root.autosize();
        Scene scene = new Scene(root, Color.LINEN);

        stage.setTitle("Row Factory Demo");
        stage.setWidth(500);
        stage.setHeight(300);
        stage.setScene(scene);
        stage.show();

        configureTable();
    }

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

        private void recheck_table() {
            tableView.setRowFactory(new Callback<TableView<RFDDomain>, TableRow<RFDDomain>>() {
            @Override
            public TableRow<RFDDomain> call(TableView<RFDDomain> paramP) {
                    return new TableRow<RFDDomain>() {

                            @Override
                            protected void updateItem(RFDDomain paramT, boolean paramBoolean) {

                                super.updateItem(paramT, paramBoolean);
                                if (!isEmpty()) {
                                    String style = "-fx-control-inner-background: #007F0E;"
                            + "-fx-control-inner-background-alt: #007F0E;";
                                    setStyle(style);
                                }
                            }
                    };
            }
    });
        }



    @SuppressWarnings("unchecked")
    private void configureTable() {
        int id =1;
        for (int i = 1; i <= 1; i++) {
            data.add(new RFDDomain(id++,"First Row", "This is for check.", 1));
            data.add(new RFDDomain(id++,"Second Row", null, 2));
            data.add(new RFDDomain(id++,"Third Row", "This is for check.", 3));
            data.add(new RFDDomain(id++,"Fourth Row", "dil", 4));
        }

        tableView.setItems(data);
                recheck_table();


        TableColumn<RFDDomain, Integer> column0 = new TableColumn<RFDDomain, Integer>("Id");
        column0.setCellValueFactory(new PropertyValueFactory<RFDDomain, Integer>("id"));

        TableColumn<RFDDomain, String> column1 = new TableColumn<RFDDomain, String>("Title");
        column1.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("name"));

        TableColumn<RFDDomain, String> column2 = new TableColumn<RFDDomain, String>("Description");
        column2.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("description"));

        TableColumn<RFDDomain, Number> column3 = new TableColumn<RFDDomain, Number>("Status");
        column3.setPrefWidth(55);
        column3.setCellValueFactory(new PropertyValueFactory<RFDDomain, Number>("status"));

        TableColumn<RFDDomain, String> column4 = new TableColumn<RFDDomain, String>("Action");
        column4.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("name"));

        tableView.getColumns().addAll(column0, column1, column2, column3, column4);

        this.root.getChildren().add(tableView);

                Button button1 = new Button("Load new Data");

                button1.setOnAction(new EventHandler<ActionEvent>() {
                    @Override public void handle(ActionEvent e) {
                        data.clear();
                        data.removeAll(data);
                        data.add(new RFDDomain(1,"First Row", "This is for check.", 1));
                        data.add(new RFDDomain(2,"Second Row", null, 2));
                        tableView.setItems(data);
                        recheck_table();
                    }
                });

                this.root.getChildren().add(button1);


    }

    /**
     * Domain Model for this demo.
     */
    public class RFDDomain {
        private SimpleIntegerProperty id = new SimpleIntegerProperty();
        private SimpleStringProperty name = new SimpleStringProperty();
        private SimpleStringProperty description = new SimpleStringProperty();
        private SimpleIntegerProperty status = new SimpleIntegerProperty();

        public RFDDomain(int id,String name, String desc, int status) {
            this.id.set(id);
            this.name.set(name);
            this.description.set(desc);
            this.status.set(status);
        }

        public int getId() {
            return id.get();
        }

        public SimpleIntegerProperty idProperty() {
            return id;
        }

        public String getDescription() {
            return description.get();
        }

        public SimpleStringProperty descriptionProperty() {
            return description;
        }

        public String getName() {
            return name.get();
        }

        public SimpleStringProperty nameProperty() {
            return name;
        }

        public int getStatus() {
            return status.get();
        }

        public SimpleIntegerProperty statusProperty() {
            return status;
        }
    }
}

回答by Ruslan Gabbazov

Try tableView.refresh(). It's available in JavaFX since Java 8u60.

试试tableView.refresh()。自 Java 8u60 以来,它在 JavaFX 中可用。

回答by PekosoG

Here is a little trick i discovered a year ago...

这是我一年前发现的一个小技巧......

myTableView.getColumns().get(0).setVisible(false);
myTableView.getColumns().get(0).setVisible(true);

This might not be the best way, but it works for me...

这可能不是最好的方法,但它对我有用......

回答by Warkst

Probably no longer relevant, but simply add

可能不再相关,但只需添加

else { setStyle(null) }

to the updateItemmethod in your custom factory - what's happening is that only as many cells are created as are absolutely required to show on the screen (prevents creating unnecessarily many UI elements). These cells are re-used when you scroll through the table by their updateItemmethod. Sometimes (in your case), an existing cell will be updated with nullto indicate the cell should be empty. You haven't implemented this case, but you should, and you should clear the cell's style.

updateItem您的自定义工厂中的方法 - 发生的事情是只创建与在屏幕上显示绝对需要的数量一样多的单元格(防止创建不必要的许多 UI 元素)。当您通过它们的updateItem方法滚动表格时,这些单元格会被重新使用。有时(在您的情况下),将更新现有单元格null以指示该单元格应为空。你还没有实现这种情况,但你应该,你应该清除单元格的样式。