通用Javafx表视图
我想在我的JAVAFX控制器中创建一个方法,它将显示表视图中数据的预览。该表视图必须足够通用,以便它可以获取对象列表并将其显示在表中。这是我的代码。通用Javafx表视图
以我控制器I具有的TableView这样
@FXML
private TableView dataPreviewTableView;
在同一个控制器向下下面我有这样
public void loadScreen(Class<?> T){
TableColumn col;
TableViewHeader headerInfo =getHeaderInfoFromTemplate(fileTemplate);
List<String> headerNames = headerInfo.getHeaderNames();
dataPreviewTableView.getItems().clear();
for(String headerName : headerNames){
col = new TableColumn (headerName.toUpperCase());
col.prefWidthProperty().bind(dataPreviewTableView.widthProperty().divide(headerNames.size()));
col.setCellValueFactory(new PropertyValueFactory<**T,Integer**>(headerName));
dataPreviewTableView.getColumns().add(col);
}
this.lblFileName.setText(fileToImport.getPath());
dataPreviewTableView.setItems(data);
}
下面一行给出错误T A方法不能被解析为一种类型
col.setCellValueFactory(new PropertyValueFactory<T,Integer>(headerName));
我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;
}
}
}
你快到了。 GenericTable类将在控制器和loadScreen方法中使用。我需要在调用loadScreen方法时传入S.Still不清楚我将如何做到这一点。 public void loadScreen(> S){ this.dataPreviewTableView = new GenericTable (this.data,this.col) dataPreviewTableView.loadTableView(); //这将实际将所有列和数据设置为基础表视图 } –
ATHER
2014-09-04 18:40:28
否您的新解决方案不是通用的。它不会工作。我喜欢创建扩展类GenericTable 的想法。唯一的是我需要弄清楚当我调用方法loadScreen(Person)或loadScreen(Equipment)或loadScreen(Vehicle)时如何使它工作。基本上这个想法是加载任何传入类型的表视图。 –
ATHER
2014-09-04 19:30:36
传入loadScreen()方法的类型需要到您的类GenericTable 。 –
ATHER
2014-09-04 19:33:10
尝试'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