I need to get the rectangle of a single column in the Table Header. Is there a way to receive this information?
Asked
Active
Viewed 668 times
-2
-
3Initially you need to try something, iterate it over and over. If stuck somewhere you need to post relevant code on SO and explain what exactly you need. – Darshan Lila May 13 '15 at 06:55
-
1`JTable#getColumnModel#getColumn(int)` – MadProgrammer May 13 '15 at 07:17
-
1*"I need to get the rectangle of a single Column in the Table Header."* Why? – Andrew Thompson May 13 '15 at 07:24
-
1You might be able to use a [JTableHeader#getHeaderRect(int)](http://docs.oracle.com/javase/8/docs/api/javax/swing/table/JTableHeader.html#getHeaderRect-int-) – aterai May 13 '15 at 10:18
1 Answers
1
i hope you want to know the size of a cell from a table header?
to get the height of the the cell you must ask the tableheader for size and take its height
JTable table;
int height = table.getTableHeader().getSize().height //JTable header is a JComponent
to get the width of a cell you have to take the width from the tableColumnModel
int column; //the index of desired column
int width = table.getTableColumnModel().getColumn(column).getWidth(); //as mentioned by @MadProgrammer
as desired i add the hints on how to get the location
int xRel = table.getTableHeader().getX();
int column; //the index of desired column
for (int c = 0; c < column; c++){
//adding all cell withds to come to your column
x = x + table.getTableColumnModel().getColumn(c).getWidth();
}
int yRel = table.getTableHeader().getY();
to get to the absolut coordinates you have to ask your frame, where it is and then add the relative coordinates
JFrame frame; //your frame
int xAbs = frame.getLocation().x + xRel;
int yAbs = frame.getLocation().y + yRel;

Martin Frank
- 3,445
- 1
- 27
- 47