2016-03-24 13 views
0

Her satır girişi için bir değer seçebileceğiniz bir birleşik hücre içeren bir tablom var. Sorun şu ki, açılır liste sadece hücre düzenlendiğinde ve her satırda her satırda gösterilmesini ister. Bu mümkün mü ve eğer mümkünse nasıl?Java FX ComboBoxTableCell Her Hücreyi Göster

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); 
Kendi tablo hücresini uygulanması (ve bunu oluşturmaya hücre fabrikası ayarlama) bunu yapabilirsiniz
+0

Bu üretir kodunuzu yayınlamanız gerekir sonuç burada. – ManoDestra

+1

@ManoDestra - Sütun koduyla güncellenen soru – Ashley

cevap

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 (combo) yönteminin açılan kutuyu göstermesinden sorumlu olduğunu varsayarsak, bu nedenle neden boş ise, setGraphic (combo) kullanarak göstermeye devam edebilirim? – Ashley

+0

"Neden boş ise?" Ile ne demek istiyorsun? Eğer reason == null'? Bu durumda yukarıdaki kod hala birleşik kutuyu gösterecektir, ancak hiçbir şey seçilmemiştir. –

+0

Teşekkürler @James_D. Evet, boş, null demek istedim. Çözümün benim senaryo için çalıştı. Çok teşekkürler – Ashley

İlgili konular