如何在JavaFX TableView中获取选定的TableCell [英] How to get selected TableCell in JavaFX TableView

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

问题描述

我正在寻找一种获取TableView控件的选定单元格的方法.请注意,我不仅需要单元格值,还需要实际的TableCell对象.人们会期望该方法:

I am looking for a way to get the selected cell of a TableView control. Note that I don't just want the cell value, I want an actual TableCell object. One would expect that the method:

tableView.getSelectionModel().getSelectedCells().get(0)

只是这样做,但是它返回一个TablePosition对象,该对象为您提供行和列信息,但是我没有从中获取TableCell对象的方法.

does just that, but it returns a TablePosition object, which gives you row and column information, but I don't see a way to get TableCell object from that.

我需要这个的原因是因为我想响应按键,但是将事件过滤器附加到TableCell上不起作用(可能是因为它不可编辑).因此,我将其附加到TableView,但随后需要获取当前选定的单元格.

The reason I need this is because I want to respond to a key press, but attaching an event filter to TableCell does not work (probably because it is not editable). So I attach it to TableView, but then I need to get the currently selected cell.

对于将来的读者:请勿将TableCell对象弄乱,除非在单元格工厂中使用.按照设计人员的预期方式使用TableView,否则您会遇到很多麻烦.如果您需要单个表中来自多个源的数据,最好创建一个新类来聚合所有数据并将其用作TableView源.

For future readers: DO NOT mess with TableCell objects, except in cell factory. Use the TableView the way designers intended, or you will be in lot of trouble. If you need data from multiple sources in single table, it is better to make a new class that aggregates all the data and use that as a TableView source.

推荐答案

我刚刚发布了使用此代码编辑单元格的答案.我认为您无法获得对实际表格单元格的引用,因为它是表格视图的内部.

I just posted an answer that uses this code to edit a Cell. I don't think you can get a reference to the actual table cell as that's internal to the table view.

tp = tv.getFocusModel().getFocusedCell();
tv.edit(tp.getRow(), tp.getTableColumn());

您的方法还返回TablePosition,因此您也可以使用它.

Your method also returns a TablePosition so you can use that as well.

这是链接 https://stackoverflow.com/a/21988562/2855515

这篇关于如何在JavaFX TableView中获取选定的TableCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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