0

I have a table view which I have populated with information, however while I can select the rows which hold data, the text is not visible. Using table.getSelectionModel().getSelectedItem(); I can confirm that the table has indeed elements inside but the text does not show up.

I have a Socket communication in my programm where the server send the client an ArrayList of elements that the Client needs to display in the TableView.

Server side:

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();
        }
    }
}

Client side:

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();
    }
}

The 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);
    }
}

And the User:

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;
    }
}

The interesting thing is that it already worked once but then I added the same mechanism I had for User and UserBuilder for other classes (so e.g. Admin and AdminBuilder) and suddenly the text did not show up in the TableView anymore.

Any and all help is greatly appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Grual
  • 73
  • 6

1 Answers1

1

Your User class needs getter methods. The PropertyValueFactory needs to access these fields.

public class User {
    private final int id;
    private final String username;
    private final 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;
    }

    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPwd() {
        return pwd;
    }

}
DJViking
  • 832
  • 1
  • 12
  • 29