2016-03-24 24 views
0

我有一個表格,它有一個組合單元格,您可以從每個行條目中選擇一個值。問題是,下拉列表僅顯示單元格正在編輯時的情況,我希望它始終顯示在每一行上。這是可能的,如果是這樣,如何?Java FX ComboBoxTableCell在每個單元格中顯示

enter image description here

TableColumn<Product, String> actionsCol = new TableColumn<Product, String>("Exception Reason"); 
    actionsCol.setCellValueFactory(new PropertyValueFactory("exceptionReason")); 
    actionsCol.setCellFactory(ComboBoxTableCell.forTableColumn(exceptionReasons)); 
    actionsCol.setOnEditCommit(
      new EventHandler<CellEditEvent<Product, String>>() { 
       @Override 
       public void handle(CellEditEvent<Product, String> t) { 
        ((Product) t.getTableView().getItems().get(t.getTablePosition().getRow())).setExceptionReason(t.getNewValue()); 
       }; 
    }); 
    actionsCol.setEditable(true); 
+0

您需要發佈您的代碼產生此結果在這裏。 – ManoDestra

+1

@ManoDestra - 問題更新列代碼 – Ashley

回答

2

您可以通過實現自己的表格單元格(並設置單元格工廠進行實例化)做到這一點:

import java.util.Random; 

import javafx.application.Application; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.stage.Stage; 

public class TableWithComboBoxExample extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     TableView<Item> table = new TableView<>(); 

     TableColumn<Item, String> itemCol = new TableColumn<>("Item"); 
     itemCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty()); 

     TableColumn<Item, Reason> reasonCol = new TableColumn<>("Reason"); 
     reasonCol.setCellValueFactory(cellData -> cellData.getValue().importanceProperty()); 

     reasonCol.setCellFactory(tc -> { 
      ComboBox<Reason> combo = new ComboBox<>(); 
      combo.getItems().addAll(Reason.values()); 
      TableCell<Item, Reason> cell = new TableCell<Item, Reason>() { 
       @Override 
       protected void updateItem(Reason reason, boolean empty) { 
        super.updateItem(reason, empty); 
        if (empty) { 
         setGraphic(null); 
        } else { 
         combo.setValue(reason); 
         setGraphic(combo); 
        } 
       } 
      }; 
      combo.setOnAction(e -> 
       table.getItems().get(cell.getIndex()).setImportance(combo.getValue())); 
      return cell ; 
     }); 

     table.getColumns().add(itemCol); 
     table.getColumns().add(reasonCol); 

     Random rng = new Random(); 
     for (int i = 1 ; i <= 100 ; i++) { 
      table.getItems().add(new Item("Item "+i, Reason.values()[rng.nextInt(Reason.values().length)])); 
     } 

     primaryStage.setScene(new Scene(table)); 
     primaryStage.show(); 
    } 

    public enum Reason { DATA_NOT_OBTAINABLE, IM_JUST_PLAIN_LAZY, I_CANT_BE_BOTHERED } 

    public static class Item { 
     private final StringProperty name = new SimpleStringProperty(); 
     private final ObjectProperty<Reason> importance = new SimpleObjectProperty<>(); 

     public Item(String name, Reason importance) { 
      setName(name); 
      setImportance(importance); 
     } 

     public final StringProperty nameProperty() { 
      return this.name; 
     } 


     public final String getName() { 
      return this.nameProperty().get(); 
     } 


     public final void setName(final String name) { 
      this.nameProperty().set(name); 
     } 


     public final ObjectProperty<Reason> importanceProperty() { 
      return this.importance; 
     } 


     public final Reason getImportance() { 
      return this.importanceProperty().get(); 
     } 


     public final void setImportance(final Reason importance) { 
      this.importanceProperty().set(importance); 
     } 



    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

我認爲setGraphic(組合)方法負責顯示組合框,所以如果原因是空的,我仍然可以通過使用setGraphic(組合)顯示它? – Ashley

+0

你是什麼意思「如果原因是空的?」。你的意思是如果'reason == null'?在這種情況下,上面的代碼仍然會顯示組合框,但沒有選擇任何東西。 –

+0

謝謝@James_D。是的,通過空我的意思是空。您的解決方案適用於我的場景。非常感謝 – Ashley

相關問題