Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Change a Column's Cursor

Did you know that you can easily customize the shape of the mouse cursor on an individual column basis? As a result, each of a table component's columns can have its own mouse cursor shape. Figure 2 shows a table component with a crosshair mouse cursor shape over the first column.

Figure 2 When the mouse cursor's hotspot enters the first column, the mouse cursor's shape changes to a crosshair.

The code to customize the mouse cursor shape involves creating an object from a subclass of the MouseMotionAdapter class (in the java.awt.event package), overriding the mouseMoved() method (in the subclass) to identify the table component's current column and set the table component's overall mouse cursor shape to the current column's shape, and registering that listener with the table component. To learn how to accomplish those tasks, examine Listing 2's DifferentColumnCursors source code.

Listing 2: DifferentColumnCursors.java

// DifferentColumnCursors.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.*;

class DifferentColumnCursors extends JFrame
{
  DifferentColumnCursors (String title)
  {
   // Pass the title to the JFrame superclass so that it appears in
   // the title bar.

   super (title);

   // Tell the program to exit when the user either selects Close
   // from the System menu or presses an appropriate X button on the
   // title bar.

   setDefaultCloseOperation (EXIT_ON_CLOSE);

   // Create a default table model consisting of headersText columns
   // and 10 rows.

   String [] headers = { "First", "Second" };

   DefaultTableModel dtm = new DefaultTableModel (headers, 5);

   // Populate all cells in the default table model with integer
   // values.

   int nrows = dtm.getRowCount ();
   int ncols = dtm.getColumnCount ();
   int n = 0;

   for (int i = 0; i < nrows; i++)
      for (int j = 0; j < ncols; j++)
        dtm.setValueAt (new Integer (n++), i, j);

   // Create a table using the previously created default table
   // model.
   // The jt variable is final, so it can be accessed from within an 
   // inner class.

   final JTable jt = new JTable (dtm);

   // Set the preferred size of the table's scrollable viewport.
   // That is an alternative to specifying setSize() for the frame 
   // window.
 
   jt.setPreferredScrollableViewportSize (new Dimension (250, 125));

   // Create an object from an anonymous subclass of the
   // MouseMotionAdapter class. Within that subclass, override the
   // mouseMoved() method.

   MouseMotionAdapter mma;
   mma = new MouseMotionAdapter ()
      {
        public void mouseMoved (MouseEvent e)
        {
          // Return the pixel position of the mouse cursor
          // hotspot.

          Point p = e.getPoint ();

          // Convert the pixel position to the zero-based
          // column index of the table column over which the
          // mouse cursor hotspot is located. The result is a
          // view-based column index. If that index refers to
          // the leftmost column, display a crosshair cursor.
          // Otherwise, display a hand cursor.

          if (jt.columnAtPoint (p) == 0)
            jt.setCursor (Cursor.getPredefinedCursor
                      (Cursor.CROSSHAIR_CURSOR));
          else
            jt.setCursor (Cursor.getPredefinedCursor
                      (Cursor.HAND_CURSOR));
        }
      };

   // Register the previous MouseMotionAdapter object as a listener
   // to mouse movement events originating from the table.

   jt.addMouseMotionListener (mma);

   // Place the table in a JScrollPane object (to allow the table to
   // be vertically scrolled and display scrollbars, as necessary).

   JScrollPane jsp = new JScrollPane (jt);

   // Add the JScrollPane object to the frame window's content pane.
   // That allows the table to be displayed within a displayed
   // scroll pane.

   getContentPane ().add (jsp);

   // Size the frame window to the preferred size of its scroll pane
   // container.

   pack ();

   // Display the frame window and all contained
   // components/containers.

   setVisible (true);
  }

  public static void main (String [] args)
  {
   // Create a DifferentColumnCursors object, which creates the GUI.

   new DifferentColumnCursors ("Different Column Cursors");
  }
}

DifferentColumnCursors achieves its "magic" by repeatedly calling JTable's setCursor() method, with the appropriate mouse cursor shape constant, in response to a mouse moved event. Before the shape changes, however, DifferentColumnCursors determines the current column by converting the mouse cursor hotspot coordinates (stored in MouseEvent parameter e) to a view-specific zero-based column index by way of JTable's columnAtPoint() method. And that is how you change a column's cursor.

  • + Share This
  • 🔖 Save To Your Account