添加数据后刷新表

时间:2022-02-21 07:24:27

I am building a football league management system, I built the user interface using javaFx, I created this class to populate the table using a database.

我正在构建一个足球联赛管理系统,我使用javaFx构建了用户界面,我创建了这个类来使用数据库填充表。

public class TableHandler {

    public static ObservableList<Team> getTeams() {
        ObservableList<Team> list = FXCollections.observableArrayList();
        DBConnection db;
        try {
            db = new DBConnection();
            String sql = "Select * from teams";
            ResultSet result = db.read(sql);
            while (result.next()) {
                list.add(new Team(result.getInt(1), result.getString(2), result.getString(3), result.getInt(4),
                        result.getDouble(5)));
            }
        } catch (Exception e) {
            e.getMessage();
        }
        return list;
    }

    public static TableView<Team> getTable(ObservableList<Team> list) {
        TableView<Team> table;
        TableColumn<Team, String> idColumn = new TableColumn<>("ID");
        idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));

        TableColumn<Team, String> nameColumn = new TableColumn<>("Name");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<Team, String> phoneNumberColumn = new TableColumn<>("phoneNumber");
        phoneNumberColumn.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));

        TableColumn<Team, Integer> pointsColumn = new TableColumn<>("Points");
        pointsColumn.setCellValueFactory(new PropertyValueFactory<>("points"));

        TableColumn<Team, Double> budgetColumn = new TableColumn<>("Budget");
        budgetColumn.setCellValueFactory(new PropertyValueFactory<>("budget"));

        table = new TableView<>();
        table.setItems(list);
        table.getColumns().addAll(idColumn, nameColumn, phoneNumberColumn, pointsColumn, budgetColumn);
        return table;

    }

and I created a button to add teams to the table by the user, what I can't figuer out is how to refresh the table when the user hit the add button, any help would be appriciated.

我创建了一个按钮,用户可以将团队添加到表中,我无法想象的是当用户点击添加按钮时如何刷新表格,任何帮助都会得到满足。

1 个解决方案

#1


2  

You don't have to. The very idea of an observable list is that the TableView observes for changes in it and renders the value change accordingly.

你不必。可观察列表的概念是TableView观察其中的变化并相应地呈现值。

The thing you have to make sure of is that you're adding elements to the collection that was actually bound to the TableView and not some other one. You didn't post the code that adds the items, so it's hard to tell, but if you're using getTeams() and then adding to that, then it's wrong (since it's a new ObservableList and not the one bound to the TableView). You should always be using table.getItems().add(...) to add items to a TableView.

您必须确保的是,您要将元素添加到实际绑定到TableView而不是其他一些的集合中。你没有发布添加项目的代码,所以很难说,但如果你使用getTeams()然后添加到那个,那就错了(因为它是一个新的ObservableList而不是绑定到TableView的那个) )。您应该始终使用table.getItems()。add(...)将项添加到TableView。

#1


2  

You don't have to. The very idea of an observable list is that the TableView observes for changes in it and renders the value change accordingly.

你不必。可观察列表的概念是TableView观察其中的变化并相应地呈现值。

The thing you have to make sure of is that you're adding elements to the collection that was actually bound to the TableView and not some other one. You didn't post the code that adds the items, so it's hard to tell, but if you're using getTeams() and then adding to that, then it's wrong (since it's a new ObservableList and not the one bound to the TableView). You should always be using table.getItems().add(...) to add items to a TableView.

您必须确保的是,您要将元素添加到实际绑定到TableView而不是其他一些的集合中。你没有发布添加项目的代码,所以很难说,但如果你使用getTeams()然后添加到那个,那就错了(因为它是一个新的ObservableList而不是绑定到TableView的那个) )。您应该始终使用table.getItems()。add(...)将项添加到TableView。