2014-09-03 89 views
0

我想在我的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)); 
+0

嘗試'col = new TableColumn '並且您應該使用S而不是T來匹配javadoc。 'TableView '其中S是基礎數據類型,T是列數據類型。我不確定這是否是您的問題,但我在代碼中使用它。 – brian 2014-09-03 23:51:46

+1

在你的方法聲明中,你有'T'作爲'Class '類型的參數名稱(即變量)。然而,在該方法的後面,您有'T'作爲類型:'PropertyValueFactory '。這是不是很清楚你打算在這裏做什麼:什麼是'T'? 「TableView」中的數據(行)的類型是什麼? – 2014-09-04 00:34:31

回答

0

我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; 
     } 

    } 

} 
+0

你快到了。 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

+0

否您的新解決方案不是通用的。它不會工作。我喜歡創建擴展類GenericTable 的想法。唯一的是我需要弄清楚當我調用方法loadScreen(Person)或loadScreen(Equipment)或loadScreen(Vehicle)時如何使它工作。基本上這個想法是加載任何傳入類型的表視圖。 – ATHER 2014-09-04 19:30:36

+0

傳入loadScreen()方法的類型需要到您的類GenericTable 。 – ATHER 2014-09-04 19:33:10