JavaFX 2.1 TableView刷新项目

use*_*048 59 java javafx tableview fxml javafx-8

我有这个常见问题,因为它似乎是.重置后,我的表视图不会刷新我的项目.我检查了数据,这是新的数据.

我从互联网尝试了多种解决方案但没有成功.

无法重置所有列,因为它添加了一个空的额外(不知道原因)和调整大小只是破坏.

我的表格不可编辑.新数据已更改.

如果我更改项目的顺序并且行更改(:|),则刷新数据.

我只是没有想法.

目前刷新代码非常简单.

ObservableList<User> data = FXCollections.observableArrayList(User.getResellers());
reseller_table.setItems(data);
Run Code Online (Sandbox Code Playgroud)

新数据再次正确.当我选择tableView时,它返回新的正确Item.

Dan*_*eón 61

解决方法:

 tableView.getColumns().get(0).setVisible(false);
 tableView.getColumns().get(0).setVisible(true);
Run Code Online (Sandbox Code Playgroud)

  • 很好,JavaFx API需要重新审视才能简化! (4认同)
  • 工作正常,非常简单,但我需要将get(0)的结果转换为TableColumn拳头. (3认同)
  • 不工作.或者我找不到`setVisible`方法.PS我将它转换为TableColumn. (2认同)
  • 这很奇怪,在java 8中我们仍然需要这样做才能工作......希望他们能尽快修复它 (2认同)

Ell*_*ltz 44

既然JavaFX 8u60可以使用(假设tableViewTableView类的一个实例):

tableView.refresh();
Run Code Online (Sandbox Code Playgroud)

从文档:

调用refresh()会强制TableView控件重新创建并重新填充填充控件可视边界所必需的单元格.换句话说,这会强制TableView更新它向用户显示的内容.这在底层数据源以TableView本身未观察到的方式发生更改的情况下非常有用.

  • 这样,我的 TableView 仅在我先单击任意位置后才会刷新,知道为什么会这样吗? (2认同)

小智 41

我有类似的问题与刷新.我的解决方案是将操作限制为ObservableList正常工作的操作bind().

假设ObservableList obsList是的基础列表TableView.

然后
obsList.clear()(从继承java.util.List<>)将不会更新TableView正确,但是.

调用setItem(obsList)无法触发刷新......但......

obsList.removeAll(obsList)(覆盖ObservableList)工作正常,因为它正确触发changeEvent.

使用全新内容重新填充列表,然后按如下方式工作:

  • obsList.removeAll(obsList);
  • obsList.add(...); //e.g. in a loop...

要么

  • obsList.removeAll(obsList);
  • FXCollections.copy(obsList, someSourceList)

最好的问候Ingo

  • 如果 someSourceList 的元素数量与 obsList 不同,则 FXCollection.copy() 不起作用。您将收到“IndexOutOfBoundsException:源不适合 dest”。在这种情况下,使用您所说的循环是要走的路。 (2认同)

Ulu*_*Biy 7

更新:
最后,在JavaFX 8u60中解决了tableview刷新问题,可以进行早期访问.


关于刷新请参阅Tableview中更新行.
关于空白列,请参阅JavaFx 2创建具有单列的TableView.基本上它不是列,即您无法选择单击此空白列项目的项目.它只是一个空白的区域,风格像一排.


更新:如果您正在更新tableView,reseller_table.setItems(data)那么您不需要使用SimpleStringProperty.如果您只更新一行/项,这将非常有用.以下是刷新表数据的完整示例:

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Dddeb extends Application {

    public static class Product {
        private String name;
        private String code;

        public Product(String name, String code) {
            this.name = name;
            this.code = code;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    private TableView<Product> productTable = new TableView<Product>();

    @Override
    public void start(Stage stage) {

        Button refreshBtn = new Button("Refresh table");
        refreshBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                // You can get the new data from DB
                List<Product> newProducts = new ArrayList<Product>();
                newProducts.add(new Product("new product A", "1201"));
                newProducts.add(new Product("new product B", "1202"));
                newProducts.add(new Product("new product C", "1203"));
                newProducts.add(new Product("new product D", "1244"));

                productTable.getItems().clear();
                productTable.getItems().addAll(newProducts);
                //productTable.setItems(FXCollections.observableArrayList(newProducts));
            }
        });

        TableColumn nameCol = new TableColumn("Name");
        nameCol.setMinWidth(100);
        nameCol.setCellValueFactory(new PropertyValueFactory<Product, String>("name"));

        TableColumn codeCol = new TableColumn("Code");
        codeCol.setCellValueFactory(new PropertyValueFactory<Product, String>("code"));

        productTable.getColumns().addAll(nameCol, codeCol);
        productTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        // You can get the data from DB
        List<Product> products = new ArrayList<Product>();
        products.add(new Product("product A", "0001"));
        products.add(new Product("product B", "0002"));
        products.add(new Product("product C", "0003"));

        //productTable.getItems().addAll(products);
        productTable.setItems(FXCollections.observableArrayList(products));

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.getChildren().addAll(productTable, refreshBtn);

        Scene scene = new Scene(new Group());
        ((Group) scene.getRoot()).getChildren().addAll(vbox);
        stage.setScene(scene);
        stage.setWidth(300);
        stage.setHeight(500);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

注意

productTable.setItems(FXCollections.observableArrayList(newProducts));
Run Code Online (Sandbox Code Playgroud)

productTable.getItems().clear();
productTable.getItems().addAll(newProducts);
Run Code Online (Sandbox Code Playgroud)

几乎相同.因此,当表刷新时,我第一次用表填充表格.它仅用于演示目的.我已经在JavaFX 2.1中测试了代码.最后,您可以(并且应该)编辑您的问题,通过移动问题答案中的代码片段来改进它.


Jav*_*ian 6

我终于找到了一个丑陋的解决方法来刷新所有行.

void refreshTable() {
    final List<Item> items = tableView.getItems();
    if( items == null || items.size() == 0) return;

    final Item item = tableView.getItems().get(0);
    items.remove(0);
    Platform.runLater(new Runnable(){
        @Override
        public void run() {
            items.add(0, item);
        }
    });
 }
Run Code Online (Sandbox Code Playgroud)