在没有中间类的情况下将数据插入JavaFX TableView [英] Inserting data to JavaFX TableView without intermediate class

查看:130
本文介绍了在没有中间类的情况下将数据插入JavaFX TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手Java程序员。
我想将ResultSet数据插入JavaFX TableView,但我不会使用中间类。我可以将ResultSet行作为对象插入TableView行吗?

I'm newbie Java programmer. I would like to insert ResultSet data to JavaFX TableView, but i wouldn't use intermediate class. Can i insert ResultSet rows to TableView rows as objects?

这是我的代码,使用中间类Unit

Here is my code with using intermediate class Unit

 public static ObservableList<Unit> getUnits() {

    final ObservableList<Unit> data = FXCollections.observableArrayList();
    if (openConnection()) {
        try {
            rs = st.executeQuery("SELECT * FROM units");
            while (rs.next()) {

                data.add(new Unit(rs.getString("id_unit"),
                        rs.getString("short_name"),
                        rs.getString("name")));
            }
        } catch (Exception ex) {
            Logger.getLogger(SQLConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    closeConnection();
    return data;
}

控制器

idUnit.setCellValueFactory(new PropertyValueFactory<Unit, String>("idUnit"));
shortNameUnit.setCellValueFactory(new PropertyValueFactory<Unit, String>("shortName"));
nameUnit.setCellValueFactory(new PropertyValueFactory<Unit, String>("name"));

unitsTableView.setItems(SQLConnect.getUnits());


推荐答案

你需要一些东西来保存数据;某些东西(你称之为中间类)将成为 TableView 的数据类型。

You need something to hold the data; that something (which you call the "intermediate class") is going to be the data type for your TableView.

它不一定必须是你创建的类,但如果你只是使用一般的东西,你的代码将更难理解。例如,您可以使用 List< String> 来保存每一行:

It doesn't necessarily have to be a class you create, but if you just use something general, your code is going to be much harder to understand. For example, you could just use a List<String> to hold each row:

TableView<List<String>> unitsTableView = new TableView<>();
idUnit = new TableColumn<List<String>, String>("Id");
idUnit.setCellValueFactory(new Callback<CellDataFeatures<List<String>, String>, ObservableValue<String>>() {
    @Override
    public ObservableValue<String> call(CellDataFeatures<List<String>, String>> data) {
        return new ReadOnlyStringWrapper(data.getValue().get(0)) ;
    }
});
shortNameUnit = new TableColumn<List<String>, String>("Short Name");
shortNameUnit.setCellValueFactory(new Callback<CellDataFeatures<List<String>, String>, ObservableValue<String>>() {
    @Override
    public ObservableValue<String> call(CellDataFeatures<List<String>, String>> data) {
        return new ReadOnlyStringWrapper(data.getValue().get(1)) ;
    }
});
nameUnit = new TableColumn<List<String>, String>("Name");
nameUnit.setCellValueFactory(new Callback<CellDataFeatures<List<String>, String>, ObservableValue<String>>() {
    @Override
    public ObservableValue<String> call(CellDataFeatures<List<String>, String>> data) {
        return new ReadOnlyStringWrapper(data.getValue().get(2)) ;
    }
});

然后

public static ObservableList<List<String>> getUnits() {

    final ObservableList<List<String>> data = FXCollections.observableArrayList();
    if (openConnection()) {
        try {
            rs = st.executeQuery("SELECT * FROM units");
            while (rs.next()) {
                List<String> unit = new ArrayList<>();
                unit.add(rs.getString("id_unit"));
                unit.add(rs.getString("short_name"));
                unit.add(rs.getString("name"));
                data.add(unit);
            }
        } catch (Exception ex) {
            Logger.getLogger(SQLConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    closeConnection();
    return data;
}

但是现在你的代码几乎完全没有你所拥有的数据的语义代表。

But now your code is almost completely devoid of the semantics of the data you are representing.

请注意,由于多种原因,您无法直接使用 ResultSet 。一个是TableView中的数据由随机访问列表表示。虽然您可以将 ResultSet 视为表示数​​据行列表,但它不实现 List 接口,并且没有任何随机访问功能(没有保证获得第5行等的方法)。另一个原因是 ResultSet 需要(或至少可能需要,具体取决于JDBC驱动程序实现)实时数据库资源。因此,您可能会在TableView占用范围的整个时间内锁定数据库表。

Note that you can't use a ResultSet directly, for a number of reasons. One is that the data in the TableView are represented by a random-access list. While you can think of ResultSet as representing a list of rows of data, it doesn't implement the List interface, and doesn't have any random-access functionality (there's no guaranteed way to get row number 5, etc.). Another reason is that a ResultSet requires (or at least may require, depending on the JDBC driver implementation) a live database resource. So you would risk locking your database table for the entire time the TableView is in scope.

为什么要避免创建数据表示类?

Why do you want to avoid creating the data representation class?

这篇关于在没有中间类的情况下将数据插入JavaFX TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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