1

In the below example, I have a table. If you hover the mouse over the table header column edges, the cursor changes to the resize cursor symbol. However, if you uncomment the line below to set the glass pane visible, the cursor no longer changes -- yet you can still resize the columns and otherwise interact with the table (e.g., select table cells, etc).

Is there a way to keep the same mouse cursor behavior when using the glass pane?

I need a solution that will work in Java 6.

public class Test extends JPanel
{
    public Test()
    {
        super(new GridLayout(1, 0));
        String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
        Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) },
                { "John", "Doe", "Rowing", new Integer(3), new Boolean(true) }, { "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
                { "Jane", "White", "Speed reading", new Integer(20), new Boolean(true) }, { "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }


    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel glass = (JPanel)frame.getGlassPane();
        //glass.setVisible(true);
        Test newContentPane = new Test();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }


    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}
martinez314
  • 12,162
  • 5
  • 36
  • 63

1 Answers1

3
  • GlassPane by default to consume every MouseEvents, have to redispatch these events, but then to use JLayer (Java7) based on JXLayer (Java6) instead of GlassPane

  • GlassPane by default never to consume KeyEvents, have to add KeyListener and call keyEvent.consume()

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Since I can still select table cells and resize the columns, it is already redispatching the mouse event, right? The only difference is mouse cursor representation. – martinez314 Dec 03 '12 at 21:49