2016-03-24 2 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。はい、空で私はnullを意味しました。あなたのソリューションは私のシナリオのために働いています。どうもありがとう – Ashley

関連する問題