Hans En
Hans En

Reputation: 926

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.

Upvotes: 6

Views: 7320

Answers (3)

Brayan Hernández
Brayan Hernández

Reputation: 11

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

Upvotes: 0

trashgod
trashgod

Reputation: 205775

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.

Upvotes: 1

StanislavL
StanislavL

Reputation: 57381

Use JTable's method

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

Upvotes: 14

Related Questions