TableView内容未显示

时间:2018-09-22 16:29:21

标签: java sockets javafx tableview

我有一个已填充信息的表格视图,但是当我可以选择保存数据的行时,该文本不可见。使用table.getSelectionModel().getSelectedItem();,我可以确认表格中确实有元素,但是文本没有显示。

我在程序中有一个Socket通信,服务器向客户端发送客户端需要在TableView中显示的元素的ArrayList。

服务器端:

private void acceptedConnection(Socket client, Connection con){
    try(ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
        ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream())){
        System.out.println("receiving request");

        String table = ois.readUTF();

        System.out.println("Preparing statement");
        PreparedStatement getTableContent = con.prepareStatement("SELECT * from " + table);

        System.out.println("Fetching results");
        ResultSet tableContentRs = getTableContent.executeQuery();

        Builder builder = null;

        switch (table){
            case "blog_user": builder = new UserBuilder();break;
        }

        assert builder != null;
        ArrayList tableContent = builder.buildArrayList(tableContentRs);


        System.out.println("Sending results");
        oos.writeObject(tableContent);
        oos.flush();
        System.out.println("Results sent");
    }catch (IOException | SQLException e){
        e.printStackTrace();
    } finally {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端:

public void loadTableContent(){
    try (Socket client = new Socket(InetAddress.getLocalHost(), 667);
         ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
         ObjectInputStream ois = new ObjectInputStream(client.getInputStream())) {

        System.out.println("Sending request");
        String selectedTable = databaseTableComboBox.getSelectionModel().getSelectedItem();
        oos.writeUTF(selectedTable);
        oos.flush();

        table.getColumns().clear();
        table.getItems().clear();

        ArrayList data = null;
        Builder builder = null;
        switch (selectedTable){
            case "blog_user": builder = new UserBuilder();data = (ArrayList<User>)ois.readObject();break;
        }

        assert builder != null;
        builder.setColumns(table);
        table.getItems().addAll(data);
        table.refresh();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}

UserBuilder:

public class UserBuilder implements Builder {
    @Override
    public ArrayList buildArrayList(ResultSet set) throws SQLException {
        ArrayList<User> users = new ArrayList<>();
        while (set.next()) users.add(new User(set.getInt(1), set.getString(2), set.getString(3)));
        return users;
    }

    @Override
    public void setColumns(TableView table) {
        TableColumn<User, String> idCol = new TableColumn<>("ID");
        TableColumn<User, String> nameCol = new TableColumn<>("Name");
        TableColumn<User, String> pwdCol = new TableColumn<>("Passwort");

        idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
        nameCol.setCellValueFactory(new PropertyValueFactory<>("username"));
        pwdCol.setCellValueFactory(new PropertyValueFactory<>("pwd"));

        table.getColumns().addAll(idCol, nameCol, pwdCol);
    }
}

用户:

public class User implements Serializable{
    private int id;
    private String username;
    private String pwd;

    public User(int id, String username, String pwd) {
        this.id = id;
        this.username = username;
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return id + " " + username + " " + pwd;
    }
}

有趣的是,它已经可以工作一次,但是后来我为其他类(例如Admin和AdminBuilder)添加了与User和UserBuilder相同的机制,突然文本不再显示在TableView中。

我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的User类需要getter方法。 PropertyValueFactory需要访问这些字段。

             geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
                @Override
                public void onKeyEntered(String key, GeoLocation location) {

                    try{
                        MyItem myItem = new MyItem(location.latitude, location.longitude);
                        if (!items.contains(myItem)) {
                            items.add(myItem);
                        }
                        Log.d("onKey","called");
                    }catch (ClassCastException e){
                        Log.d("classCastException","");
                    }

                }

                @Override
                public void onKeyExited(String key) {

                }

                @Override
                public void onKeyMoved(String key, GeoLocation location) {

                }

                @Override
                public void onGeoQueryReady() {
                   // countingMarkers = 0;
                    parseJsonToList();
                }

                @Override
                public void onGeoQueryError(DatabaseError error) {

                }
            });