2

I'm following a tutorial by Oracle to create a TableView in JavaFX. In this screenshot, I've copied and pasted the code and I get several errors, which aren't mentioned in the tutorial anywhere.

enter image description here

The warning on the TableView says:

TableView is a raw type. References to generic type TableView<​S> should be parameterized

the warning on the TableColumn says:

TableColumn is a raw type. References to generic type TableColumn<​S,T> should be parameterized

and the warning on the addAll method says:

Type safety: The method addAll(Object...) belongs to the raw type ObservableList. References to generic type ObservableList<​E> should be parameterized

I looked at the documentation and saw that they do require parameters (which is confusing because the tutorial didn't mention this) but I don't understand what they are supposed to be. If I do

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

and

TableColumn<String, String> tableCol = new TableColumn<>(...);

I still get a warning on the addAll method, which says:

Type safety: A generic array of TableColumn<​String,?> is created for a varargs parameter

I really can't understand what is expected for the type parameters. I know I can suppress the warnings, but that seems like bad practice. What am I supposed to do instead?

1
  • TableView is normally some Object Commented Dec 12, 2018 at 22:03

1 Answer 1

5

TableView requires data objects that it will try to represent in each row. So as per your example, you are trying to display a list of Persons in your table. So your tableView reference type will be of type Person. like

TableView<Person> tableView = new TableView<>();

And now each column represents the data type of the information you want to display. In your case all are Strings. But it can be varied as per the data that you want to show.

Consider for example the below Person object.

public class Person{
     private StringProperty firstName = new SimpleStringProperty();
     private StringProperty lastName = new SimpleStringProperty();
     private IntegerProperty age = new SimpleIntegerProperty();
     private DoubleProperty weight = new SimpleDoubleProperty();
}

In the above person object if you want to represent the data in each column your column declarations will be like..

TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
TableColumn<Person, Integer> ageCol = new TableColumn<>("Age");
TableColumn<Person, Double> weightCol = new TableColumn<>("Weight");
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I created my GUI last and the things I want to store in the TableView are not Objects, they are instead just stored in a custom data structure that has two arrays and matching data is stored in the same index in both arrays (i.e. I have name[] and age[], and age[0] goes with name[0]). Is there any way I can use a TableView with this, or should I convert these all to Objects instead?
I recommend to first convert them to objects and set them to TableView. This way things will be far simpler and you can have a lot of control on displaying the data.

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.