2014-10-27 63 views
0

我一直在爲此奮戰一段時間,最後想到我需要一些幫助。用Java-FX固定單元大小表

我想創建一個表,其中每個單元顯示特定的顏色。這是基於用戶選擇的圖像動態創建的。

我得到它與TilePane一起工作,但由於表的大小,它運行速度非常慢。

目前,我正在嘗試使用TableView進行工作。 TableView正在創建,但尚未填充。我創建TableView和我的自定義TableCell類的代碼如下。

我的主要問題是:這是最好的方式去做這件事?如果是這樣,我做錯了什麼?

非常感謝您提供我總是在這裏看到的真棒協助。

代碼來創建的TableView:

private ObservableList<ObservableList<Stitch>> stitchList; 
private TableView<Stitch> pattern; 

@Override 
protected TableView<Stitch> call() throws Exception { 
    for (int i=0; i< stitchList.size(); i++) { 
     TableColumn<Stitch, Color> column = new TableColumn<>(); 
     column.setCellValueFactory(new Callback<CellDataFeatures<Stitch, Color>, ObservableValue<Color>>() { 

      public ObservableValue<Color> call(CellDataFeatures<Stitch, Color> stitch) { 
       return stitch.getValue().getDisplayColorProperty(); 
      } 
      }); 

     column.setCellFactory(new Callback<TableColumn<Stitch, Color>, TableCell<Stitch, Color>>() { 
      @Override public TableCell<Stitch, Color> call(TableColumn<Stitch, Color> list) { 
       return new StitchCell(); 
      } 
     }); 

     //Set column sizes 
     column.setMinWidth(10); 
     column.setMaxWidth(10); 
     pattern.getColumns().add(column); 
    } 

    return pattern; 
} // End Call 

代碼自定義單元格類

public class StitchCell extends TableCell<Stitch, Color> { 

@Override 
protected void updateItem(Color color, boolean empty) { 
    super.updateItem(color, empty); 

    if (empty || color == null) { 
     this.setStyle("-fx-background-color: white"); 
    } else { 
     this.setStyle("-fx-background-color: #" + Integer.toHexString(color.hashCode())); 
    } 
} 
} 
+0

您是否將任何項目添加到表中的任何位置?您顯示添加列的代碼,但沒有代碼實際添加項目。 – 2014-10-27 13:44:17

+0

我在想如何去做。我有一個嵌套的可觀察列表的二維數組,但我不知道如何將數據添加到表 – 2014-10-27 16:42:41

回答

0

當你創建一個TableView<S>S是表的每一行中顯示的對象的類型。該表的items屬性是ObservableList<S>,其中包含項目集合,每個項目都顯示在表格行中。

如果您的數據的格式爲ObservableList<ObservableList<Stitch>> stitchList,那麼您需要一個TableView<ObservableList<Stitch>>。 (換句話說,SObservableList<Stitch>。)stitchList的每個元素ObservableList<Stitch>表示表中的一行。

TableView不是用ObservableList<ObservableList>作爲表示數據的主要用例,因此您需要做一點工作才能使cellValueFactory正常工作。如果你確信每一行有相同數量的項目,可以或許減少這種有點,但你需要像下面這樣:

TableView<ObservableList<Stitch>> pattern = new TableView<>(); 
ObservableList<ObservableList<Stitch>> stitchList = ... ; 

// Set items (i.e. rows) in table: 
pattern.setItems(stitchList); 

// iterate through all rows: 

for (ObservableList<Stitch> row : pattern) { 
    // if this row contains more elements than columns we have already created, 
    // (must be true on first row, may be true on subsequent rows if data not rectangular) 
    // create a new column for each additional element 
    for (int i = pattern.getColumns().size(); i < row.size() ; i++) { 
     TableColumn<ObservableList<Stitch>, Color> column = new TableColumn<>(); 
     final int columnIndex = i ; 
     column.setCellValueFactory(new Callback<CellDataFeatures<ObservableList<Stitch>, Color>, ObservableValue<Color>>() { 
      @Override 
      public ObservableValue<Stitch> call(CellDataFeatures<ObservableList<Stitch>, Stitch>> stitch) { 
       return stitch.getValue() // the row value, i.e. an ObservableList<Stitch> 
          .get(columnIndex) // the Stitch for this cell 
          .getDisplayColorProperty(); 
      } 
     }); 

     column.setCellValueFactory(new Callback<TableColumn<ObservableList<Stitch>, Color>, TableCell<ObservableList<Stitch>, Color>>() { 
      @Override 
      public TableCell<ObservableList<Stitch>, Color> call(TableColumn<ObservableList<Stitch>, Color> col) { 
       return new StitchCell(); 
      } 
     }); 

     column.setMinWidth(10); 
     column.setMaxWidth(10); 
     pattern.getColumns().add(column); 
    } 
} 

如果您使用的是Java 8,可以更換Callback小號用lambda表達式,並擺脫代碼中最醜的部分:

for (ObservableList<Stitch> row : pattern) { 
    // if this row contains more elements than columns we have already created, 
    // (must be true on first row, may be true on subsequent rows if data not rectangular) 
    // create a new column for each additional element 
    for (int i = pattern.getColumns().size(); i < row.size() ; i++) { 
     TableColumn<ObservableList<Stitch>, Color> column = new TableColumn<>(); 
     final int columnIndex = i ; 
     column.setCellValueFactory(rowData -> 
      rowData.getValue() // the row value, i.e. an ObservableList<Stitch> 
       .get(columnIndex) // the Stitch for this cell 
       .getDisplayColorProperty()); 

     column.setCellValueFactory(col -> new StitchCell()); 

     column.setMinWidth(10); 
     column.setMaxWidth(10); 
     pattern.getColumns().add(column); 
    } 
} 
+0

工作,謝謝! – 2014-10-27 19:03:26