简体   繁体   English

获取所选JTable单元的位置?

[英]Get Position of a selected JTable cell?

I have a JFrame with a few JTables. 我有一个带有几个JTable的JFrame。 On one certain JTable, I need the position of the cell in the JFrame that is selected (selection done via code), as soon as it is selected. 在一个特定的JTable上,我需要在选择的JFrame中选择单元格的位置(通过代码完成选择)。 I would like to draw something here on a Glass Pane. 我想在Glass Pane上画一些东西。 How can I accomplish this? 我怎么能做到这一点?

Point p = gui.rerouteTable.getLocation();
SwingUtilities.convertPointToScreen(p,gui.rerouteTable);

I thought this could get me the upper left hand corner of the table. 我以为这可以让我在桌子的左上角。 And via Cell Height and the SelectionListener I could claculate the position i need. 通过Cell Height和SelectionListener,我可以克服我需要的位置。 But i ca´t even get the uppper left hand corner of the table. 但我甚至得不到桌子的左手角落。 Why not? 为什么不? The gui.rerouteTable.getLocation() return (0,0) so obviously the convertPointToScreen is not working correctly. gui.rerouteTable.getLocation()返回(0,0)所以很明显convertPointToScreen无法正常工作。

使用JTable的方法

public Rectangle getCellRect(int row, int column, boolean includeSpacing)

If you don't need the glass pane for other reasons, also consider a custom cell renderer that uses the value of isSelected to condition the renderer's appearance. 如果由于其他原因不需要玻璃窗格,还可以考虑使用isSelected值来调整渲染器外观的自定义单元格渲染器。

Addendum: Each time a TableCellRenderer is invoked for a particular cell, the parameter named value is a reference to the Object obtained from the model for that cell. 附录:每次为特定单元TableCellRendererTableCellRenderer ,名为value的参数是对从该单元格的模型获取的Object的引用。

With this code you can get the x and y coordinates of a cell in a table: 使用此代码,您可以获取表格中单元格的x和y坐标:

private void jtTableMouseClicked(java.awt.event.MouseEvent evt) {                                      
    int x = jtTable.getSelectedRow();
    int y = jtTable.getSelectedColumn();
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM