I missed the part where you had Class<?>
as James said in the comments. Maybe you plan to get the type with reflection somehow?
This is a snip from code that's similar to how I do it.
public class GenericTable<S> extends TableView {
public GenericTable(ObservableList data, String col) {
super(data);
TableColumn<S, String> tc = new TableColumn<>(col);
tc.setCellValueFactory(new PropertyValueFactory<>(col));
getColumns().add(tc);
}
}
I like using <S, String>
because it will show most anything, strings, ints, reals.
Maybe you just need to do something like this, no type info required. I think the <S,T>
is mainly for compile time checks. Since you want any type, it doesn't help you.
package tablegeneric;
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TableGeneric extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<Data1> data1s = FXCollections.observableArrayList();
ObservableList<Data2> data2s = FXCollections.observableArrayList();
data1s.addAll(new Data1("str1"), new Data1("str2"), new Data1("str3"));
data2s.addAll(new Data2(1, 1), new Data2(2, 2), new Data2(3, 3));
TableView tv = new TableView();
Button btn = new Button("click to change table data");
btn.setOnAction(evt -> {
if (tv.getItems() == data1s) showScreen(tv, data2s, "i", "d");
else showScreen(tv, data1s, "str");
});
Scene scene = new Scene(new VBox(tv, btn), 200, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void showScreen(TableView tv, ObservableList data, String... cols) {
tv.setItems(data);
tv.getColumns().clear();
for (String s : cols) {
TableColumn tc = new TableColumn(s);
tc.setCellValueFactory(new PropertyValueFactory(s));
tv.getColumns().add(tc);
}
}
public class Data1 {
private SimpleStringProperty str;
public Data1(String s) {
this.str = new SimpleStringProperty(s);
}
public SimpleStringProperty strProperty() {
return str;
}
}
public class Data2 {
private SimpleIntegerProperty i;
private SimpleDoubleProperty d;
public Data2(int i, double d) {
this.i = new SimpleIntegerProperty(i);
this.d = new SimpleDoubleProperty(d);
}
public SimpleIntegerProperty iProperty() {
return i;
}
public SimpleDoubleProperty dProperty() {
return d;
}
}
}
` where S is the underlying data type and T is the column data type. I'm not sure if this is your problem but I use it in my code.