如何在表视图中的ComboBoxTableCell中选择值

时间:2015-10-01 10:20:13

标签: javafx combobox javafx-2 tableview

我已尝试使用此代码在组合框中选择值,此代码可用。

ComboBoxTableCell

但是如何在TableView ComboBox内的columnmain2.setCellFactory(ComboBoxTableCell.forTableColumn(names.toString())); 中获取所选值?

使用下面的代码我在表格视图中获得else if

$(".button").click(function() {
  var self = this, // Cache
    div1 = $('.div1', self), // Cache
    div2 = $('.div2', self), // Cache
    div3 = $('.div3', self), // Cache
    divtemp = $('.divtemp', self); // Cache

  if (div1.is(':visible')) {
    div1.hide();
    div2.show();
  } else if (div2.is(':visible')) { // Use else if
    div2.hide();
    div3.show();
    divtemp.show();
  } else if (div3.is(':visible')) { // Use else if
    div1.show();
    div3.hide();
    divtemp.hide()
  }
});

以及如何在组合框表格单元格中的表格视图中选择值?

1 个答案:

答案 0 :(得分:3)

当用户退出该组合框表格单元格的编辑模式时,您可以获得组合框选定值。即提交新值时。您需要使用tablecolumn.setOnEditCommit()方法。这是一个完整的可运行示例代码(用于ComboBoxTableCell演示的MCVE):

public class ComboBoxTableCellDemo extends Application
{
    private TableView<Person> table = new TableView<>();
    private final ObservableList<Person> data
            = FXCollections.observableArrayList(
                    new Person( "Bishkek" ),
                    new Person( "Osh" ),
                    new Person( "New York" ),
                    new Person( "Madrid" )
            );

    @Override
    public void start( Stage stage )
    {
        TableColumn<Person, String> cityCol = new TableColumn<>( "City" );
        cityCol.setMinWidth( 200 );
        cityCol.setCellValueFactory( new PropertyValueFactory<>( "city" ) );
        cityCol.setCellFactory( ComboBoxTableCell.<Person, String>forTableColumn( "Bishkek", "Osh", "New York", "Madrid" ) );
        cityCol.setOnEditCommit( ( TableColumn.CellEditEvent<Person, String> e ) ->
        {
            // new value coming from combobox
            String newValue = e.getNewValue();

            // index of editing person in the tableview
            int index = e.getTablePosition().getRow();

            // person currently being edited
            Person person = ( Person ) e.getTableView().getItems().get( index );

            // Now you have all necessary info, decide where to set new value 
            // to the person or not.
            if ( ok_to_go )
            {
                person.setCity( newValue );
            }
        } );

        table.setItems( data );
        table.getColumns().addAll( cityCol );
        table.setEditable( true );

        stage.setScene( new Scene( new VBox( table ) ) );
        stage.show();
    }


    public static class Person
    {
        private String city;

        private Person( String city )
        {
            this.city = city;
        }


        public String getCity()
        {
            return city;
        }


        public void setCity( String city )
        {
            System.out.println( "city set to new value = " + city );
            this.city = city;
        }
    }


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

}