我mi正如詹姆斯在評論中所說的那樣,檢驗了你所在的部分Class<?>
。也許你打算以某種方式反思類型?
這是一段代碼片段,與我如何做相似。
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);
}
}
我喜歡用<S, String>
,因爲它會顯示任何東西,串,整型,實數。
也許你只需要做這樣的事情,不需要輸入任何信息。我認爲<S,T>
主要用於編譯時檢查。既然你想要任何類型,它不會幫助你。
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;
}
}
}
嘗試'col = new TableColumn'並且您應該使用S而不是T來匹配javadoc。 'TableView
'其中S是基礎數據類型,T是列數據類型。我不確定這是否是您的問題,但我在代碼中使用它。 – brian 2014-09-03 23:51:46在你的方法聲明中,你有'T'作爲'Class >'類型的參數名稱(即變量)。然而,在該方法的後面,您有'T'作爲類型:'PropertyValueFactory'。這是不是很清楚你打算在這裏做什麼:什麼是'T'? 「TableView」中的數據(行)的類型是什麼? –
2014-09-04 00:34:31