0

Hi, I have tried the following code to display only one column of mysql table in tableview of javafx, but it's not working.

TableView table = new TableView();
private static ObservableList<ObservableList> data = FXCollections.observableArrayList();

TableColumn firstNameCol = new TableColumn("Username");
table.getColumns().addAll(firstNameCol);
firstNameCol.prefWidthProperty().bind(table.widthProperty().divide(1));
firstNameCol.setCellValueFactory(new PropertyValueFactory("Username"));

Connection conn_mysql;
conn_mysql = DBConnector.connect();
String SQL = "SELECT Username_name from Accounts";
ResultSet rs = conn_mysql.createStatement().executeQuery(SQL);

while (rs.next()) { 
    ObservableList<String> row = FXCollections.observableArrayList();
    row.add(rs.getString("Username_name"));
    data.add(row);
}

table.setItems(data);
9
  • Can you be more explanatory adding what exactly is not working? If you are looking at exceptions, add the stacktrace to the question. Commented Sep 25, 2015 at 9:22
  • @ItachiUchiha: Column Heading Username is working, but the row values i.e getString("Username_name") is not displaying in tableview. Commented Sep 25, 2015 at 9:46
  • Rows [User1] Rows [User2] Rows [User3] Rows [User4] Data [[User1], [User2], [User3], [User4]] Commented Sep 25, 2015 at 9:48
  • Have you tried to rename your firstNameCol to new TableColumn("Username_name")? As i read the oracle docs for PropertyValue it would search for this as your not using a pojo for your username Commented Sep 25, 2015 at 9:51
  • Yes I have tried that too.. Commented Sep 25, 2015 at 9:58

1 Answer 1

1

Why your code doesn't work

The cellValueFactory is a function that maps the item representing each row to the value that should be displayed in the cell for that row and that column. This function is represented as a Callback. PropertyValueFactory is a convenience implementation that is equivalent either to

cellData -> cellData.getValue().xxxProperty()

or

cellData -> new ReadOnlyObjectWrapper(cellData.getValue().getXxx())

where xxx is the name specified in the PropertyValueFactory and cellData is a CellDataFeatures.


Improving your code (for clarity as to what is happening and to help fix it)

Your code would be much improved if you didn't use raw types. I.e. you should do

TableView<ObservableList<String>> table = new TableView<>();
private static ObservableList<ObservableList<String>> data = FXCollections.observableArrayList();

TableColumn<ObservableList<String>, String> firstNameCol = new TableColumn<>("Username");

Here you actually specify the types represented by the TableView and TableColumn. Each row is represented by an ObservableList<String> and the value in each cell in the column is represented by a String.

Now it should be clear why your code doesn't work. The PropertyValueFactory gets the value for a row, in effect

ObservableList<String> row  = ... ;

and tries to call either

row.UsernameProperty()

or

row.getUsername()

to get the value for the column. Neither of those methods exist.


Fixing the problem

So you should just set the cellValueFactory to something that looks up the first element of the list representing the row:

firstNameCol.setCellValueFactory(cellData -> {
    ObservableList<String> rowValue = cellData.getValue();
    String cellValue = rowValue.get(0);
    return new ReadOnlyStringWrapper(cellValue);
});
Sign up to request clarification or add additional context in comments.

3 Comments

firstNameCol.setCellValueFactory(cellData -> cellData.getValue().Username_nameProperty());
For above code , error message is shown: cannot find symbol method getValue()
In my code you get that error message? The code you posted in the first comment won't work for exactly the reasons explained in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.