package com.oracle.javafx;

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.ComboBoxTableCell; 
import javafx.scene.layout.Region; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.util.Callback; 


public class ComboBoxTest extends Application { 

    @Override 
    public void start(final Stage primaryStage) throws Exception { 

        primaryStage.centerOnScreen(); 
        primaryStage.setHeight(200); 
        primaryStage.setWidth(300); 

        ObservableList<String> list1 = FXCollections.observableArrayList(); 
        list1.add("one"); 
        list1.add("two"); 
        list1.add("three"); 

        ObservableList<String> list2 = FXCollections.observableArrayList(); 
        list2.add("four"); 
        list2.add("five"); 
        list2.add("six"); 
        list2.add("seven"); 
        list2.add("eight"); 
        list2.add("nine"); 

        TableView<Row> table = new TableView<>(); 
        table.setEditable(true); 
        Row row = new Row(); 
        row.possibleRowValues.addAll(list1); 

        TableColumn<Row, String> column = new TableColumn<>("col"); 
        column.setCellValueFactory(p -> p.getValue().selectedValue); 
        column.setCellFactory(new Callback<TableColumn<Row, String>, TableCell<Row, String>>() 
        { 
            @Override 
            public TableCell<Row, String> call(final TableColumn<Row, String> param) 
            { 
                return new ComboBoxTableCell(row.possibleRowValues); 
            } 
        }); 

        table.getColumns().addAll(column); 
        table.setItems(FXCollections.observableArrayList(row)); 

        Button button = new Button("Change combo contents"); 
        button.setOnAction(event -> { 
            if (row.possibleRowValues.size() == 3 ) { 
                row.possibleRowValues.clear(); 
                row.possibleRowValues.addAll(list2); 
            } else { 
                row.possibleRowValues.clear(); 
                row.possibleRowValues.addAll(list1); 
            } 
        }); 

        VBox box = new VBox(20, table, button ); 
        box.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE); 
        primaryStage.setScene(new Scene( new StackPane(box) )); 

        primaryStage.show(); 

    } 

    private class Row 
    { 
        StringProperty selectedValue = new SimpleStringProperty(""); 
        ObservableList<String> possibleRowValues = FXCollections.observableArrayList(); 
    } 

    public static void main(String[] args) throws Exception { 
        launch(args); 
    } 

} 