6

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.

Hans En
  • 906
  • 5
  • 15
  • 32
  • Duplicate : http://stackoverflow.com/questions/14416188/jtable-how-to-get-selected-cells – Silviu Burcea Oct 22 '13 at 10:53
  • @SilviuBurcea that is a different question. This question is about the location in the frame, not what is selected. – jzd Oct 22 '13 at 10:55

3 Answers3

13

Use JTable's method

public Rectangle getCellRect(int row, int column, boolean includeSpacing)
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Will this return the postion in the Table itself or in the entire Window? – Hans En Oct 22 '13 at 10:54
  • @HansEn I don't know, but check it out and if it's relative to table, add the table offset relative to window. – Silviu Burcea Oct 22 '13 at 10:57
  • In table. You can use public static void convertPointToScreen(Point p,Component c) and public static Point convertPoint(Component source,int x, int y,Component destination) methods of SwingUtilitites – StanislavL Oct 22 '13 at 10:58
  • there are another two ways, see Oracle tutorial How to use Tables or List - about tooltip, or ListSelectionListener with SINGLE_..., – mKorbel Oct 22 '13 at 11:03
  • The Problem is the GridBagLyout which changes the sizes of the Tables. And of Course the Splitpanes teh Jtbales are placed in. So the relative position is not a fixed value. .convertPoint(...) will not work, because I do not have the component of the cell.. – Hans En Oct 22 '13 at 11:25
1

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks. I have implemented a renderer. But i do need the glass pane. – Hans En Oct 22 '13 at 11:33
  • OK; here's an [example](http://stackoverflow.com/a/14429388/230513) of using `getCellRect()`. – trashgod Oct 22 '13 at 11:38
  • But i do need the glass pane ---> maybe you would need layout JLabel over JTables cell, nobody knows – mKorbel Oct 22 '13 at 11:44
  • My cellrenderer does extend JLabel. But If I try to to (JLabel)Jtable.getModel.getValueAt(0,0) I get an Exception String cannot be cast to JLabel. – Hans En Oct 22 '13 at 11:50
  • It sounds like the `value` seen by the renderer's `getTableCellRendererComponent()` method is a `String`, but the default render itself is a `JLabel`, which is reused for each applicable cell. More above. – trashgod Oct 22 '13 at 12:09
0

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();
} 
displayName
  • 13,888
  • 8
  • 60
  • 75