如何在表视图中的ComboBoxTableCell中选择值 [英] How to get values selected in ComboBoxTableCell in Table View

查看:229
本文介绍了如何在表视图中的ComboBoxTableCell中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have tried this code to get values selected in a combo box and this code works.

String cate = category.getValue().toString();

但是如何在 ComboBoxTableCell 里面 TableView

使用下面的代码我得到一个 ComboBox inside Table view

Using below code i am getting a ComboBox inside Table view

columnmain2.setCellFactory(ComboBoxTableCell.forTableColumn(names.toString()));

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

and how to get the values selected inside a Table view in combo box table cell?

推荐答案

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

You can get the combobox selected value, when the user exits edit mode for that combobox table cell. Namely when the new value is committed. You need to use tablecolumn.setOnEditCommit() method. Here is a full runnable sample code (MCVE for ComboBoxTableCell Demo):

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 );
    }

}

这篇关于如何在表视图中的ComboBoxTableCell中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆