GuidesUser Code: Place a Hyperlink Hand Cursor on a JTable Header

User Code: Place a Hyperlink Hand Cursor on a JTable Header

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:

  1. Create a JTable with a model.
  2. Get the existing table header column model.
  3. Create a subclassed table header object and set it to table header.
  4. Create a subclass of JTableheader and override the method processMouseMotionEvent.
  5. Get the column hit from the coordinates of the mouse event.
  6. Check if it matches with the required column; if ‘yes’, set the Hand Cursor.
  7. 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

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories