Do you want to show a Hand Cursor when a Hyperlink label is placed on a table header?
To place a simulated Hyperlink on the JTable Header,
you need to change the cursor to highlight the hyperlink effect of showing the
Hand cursor when the cursor is on top of the hyperlink. The following lines of code
guide the developer to simulate it.
The steps you need to follow are:
- Create a JTable with a model.
- Get the existing table header column model.
- Create a subclassed table header object and set it to table header.
- Create a subclass of JTableheader and override the method
processMouseMotionEvent
. - Get the column hit from the coordinates of the mouse event.
- Check if it matches with the required column; if ‘yes’, set the Hand Cursor.
- Else pass the event to ‘super’ to handle it.
For further resources refer to the
author’s site. If you have any questions mail to: range_gowda@hotmail.com>
*/ JTable customTable = new JTable(customTModel); TableColumnModel tcm = customTable.getTableHeader().getColumnModel(); customTable.setTableHeader(new MyTableHeader(tcm)); class MyTableHeader extends JTableHeader { public MyTableHeader(TableColumnModel tcm) { super(tcm); } public void processMouseMotionEvent(MouseEvent me) { int column = customTable.getTableHeader().columnAtPoint(me.getPoint()); if(column == 2) setCursor(new Cursor(Cursor.HAND_CURSOR)); else super.processMouseMotionEvent(me); } } // end of class MyTableHeader