java 从 JavaFX 2.0 中的 TableView 读取选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13393301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:33:02  来源:igfitidea点击:

Read selection from TableView in JavaFX 2.0

javajavafx-2javafx

提问by carson314

I am trying to get the selection from a TableView in JavaFX 2.0. what happens is that I need to get the value of the row you selected in tableview I hope someone can help me

我正在尝试从 JavaFX 2.0 中的 TableView 中获取选择。发生的事情是我需要获取您在 tableview 中选择的行的值我希望有人可以帮助我

As would be placed on a table

就像放在桌子上一样

I mean I want to get the data you select and if there is any way to handle an event to get the selected row automatically

我的意思是我想获取您选择的数据,以及是否有任何方法可以处理事件以自动获取所选行

采纳答案by invariant

you need ChangeListenerand Clipboardto accomplish your task :)

你需要ChangeListenerClipboard来完成你的任务:)

Example Code :

示例代码:

Clipboard clipboard = Clipboard.getSystemClipboard(); 
  // add listner to your tableview selecteditemproperty   
userTable.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
              // this method will be called whenever user selected row
            @override
             public void chnaged(ObservableValue observale, Object oldValue,Object newValue) {
               UserClass selectedUser = (UserClass)newValue;
             ClipboardContent content = new ClipboardContent();
                 // make sure you override toString in UserClass
             content.putString(selectedUser.toString()); 
             clipboard.setContent(content); 
            }
            });

回答by zhujik

If i understood you correctly, you want to retrieve the row number of the cell that is currently selected inside a TableView.

如果我理解正确,您想检索当前在 TableView 中选择的单元格的行号。

To do this, request the SelectionModel of the TableView:

为此,请请求 TableView 的 SelectionModel:

    // tv is of type TableView
    TableView.TableViewSelectionModel selectionModel = tv.getSelectionModel();
    ObservableList selectedCells = selectionModel.getSelectedCells();
    TablePosition tablePosition = (TablePosition) selectedCells.get(0);
    int row = tablePosition.getRow(); // yields the row that the currently selected cell is in

回答by Perneel

It's still not clear to me what you are trying to do...

我仍然不清楚你想做什么......

However, getting the selected row:

但是,获取所选行:

final Countries selectedCountry = tblCountries.getSelectionModel().getSelectedItem();

If there is a need that another pane becomes visible or another window to show just add an eventhandler to the onclicked property or such?

如果需要另一个窗格可见或另一个窗口显示,只需将事件处理程序添加到 onclicked 属性等?

Is it that what you mean?

你是这个意思吗?

回答by Java Man

    tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
    @Override
    public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
        //Check whether item is selected and set value of selected item to Label
        if(tableview.getSelectionModel().getSelectedItem() != null) 
        {    
           TableViewSelectionModel selectionModel = tableview.getSelectionModel();
           ObservableList selectedCells = selectionModel.getSelectedCells();
           TablePosition tablePosition = (TablePosition) selectedCells.get(0);
           Object val = tablePosition.getTableColumn().getCellData(newValue);
           System.out.println("Selected Value" + val);
         }
         }
     });

Using this code you can get the selected value from JAVAFX TABLEVIEW Cell.

使用此代码,您可以从 JAVAFX TABLEVIEW 单元中获取选定的值。

Thanks..

谢谢..