简体   繁体   中英

Get Position of a selected JTable cell?

I have a JFrame with a few JTables. 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. I would like to draw something here on a 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. 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.

使用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.

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.

With this code you can get the x and y coordinates of a cell in a table:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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