2

I want to populate TableView with String, String I have only two columns. I couldn't do it. What I tried so far is:

ObservableList<String> data = FXCollections.observableArrayList();
for (blabla){
data.addAll(Id,Name);
}
tableView.getItems().addAll(data);

and nothing shows on table. But when I do tableView.getItems() and print it, I see the data I've added. But on table, nothing is shown.

I create my columns as follows:

TableColumn idColumn = new TableColumn("Id");

TableColumn nameColumn = new TableColumn("Name");

I see some examples on the internet in which an object is created to populate the TableView but I don't want to create a class just for a table view. Is there any other possible ways to accomplish this?

Edit:

I just want to have something like:

ObservableList<String,String> data = FXCollections.observableArrayList(); 

for(int i = 1; i<blabla i++){
data.add((i)+"S",myList.get(i));
}
3
  • Disclaimer: I don't know JavaFX. I guess that 'blabla' in your first snippet may be important - can you please provide simple version of that? Also: looking at javadocs, I would try using setItems(...) instead of getItems().addAll(...). See example in "Creating TableView": docs.oracle.com/javase/8/javafx/api/javafx/scene/control/… Commented Dec 19, 2017 at 11:20
  • As I said in the question I dont want to create a class for this; as you can see in javadocs they create a class in example to put data in observableList. Maybe its the only way. I asked because I think there should be another way Commented Dec 19, 2017 at 11:26
  • 3
    Here's an old example that populates a table view from a tab-delimited file, using just lists of strings. Why don't you want to create a class to represent your data? That is the usual way to do things in Java. Commented Dec 19, 2017 at 12:39

1 Answer 1

3

First, you have to create your table'c columns in the FXML file or from code. After, you have to create cell value factory which will generate your columns with your data.

For example:

@FXML
private TableView<String> tableView;

@FXML
private TableColumn<String, String> columnOne;

@FXML
private TableColumn<String, String> columnTwo;

columnOne.setCellValueFactory(c -> new SimpleStringProperty(new String("123")));

columnTwo.setCellValueFactory(c -> new SimpleStringProperty(new String("456")));

Without FXML:

private TableView<String> table = new TableView<String>();

TableColumn columnOne = new TableColumn("One");
TableColumn columnTwo = new TableColumn("Two");

table.getColumns().addAll(columnOne, columnTwo);

columnOne.setCellValueFactory(c -> new SimpleStringProperty(new String("123")));
columnTwo.setCellValueFactory(c -> new SimpleStringProperty(new String("456")));

table.getItems().addAll("Column one's data", "Column two's data");
Sign up to request clarification or add additional context in comments.

1 Comment

How can i do this without FXML?

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.