Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4114073

Can't set cursor in JPanel or JComponent

XMLWordPrintable

    • 1.1.8
    • generic, x86
    • generic, windows_95, windows_nt
    • Not verified



        Name: rm29839 Date: 02/20/98


        I am unable to set the cursor for a JPanel when it's added to the
        content pane of a JFrame. No problem setting the cursor for JFrame's or
        other Swing components, just this one particular case. I'm using Swing
        0.7 with JDK 1.2beta2. Here's a repro case:

        /* CursorBug.java
         */
        import java.awt.*;
        import java.awt.event.*;
        import com.sun.java.swing.*;

        public class CursorBug {

            // Main entry point
            public static void main(String s[]) {
                // Create app panel
                CursorBugPanel panel = new CursorBugPanel();

                // Create a frame for app
                JFrame frame = new JFrame("CursorBug");
                frame.setBackground(Color.blue);
        // Sets cursor for frame, no problem
        // frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        //
                // Add a window listener for window close events
                frame.addWindowListener(new WindowAdapter() {
                     public void windowClosing(WindowEvent e) {System.exit(0);}
                });

                // Add app panel to content pane
                frame.getContentPane().add(panel);

                // Set initial frame size and make visible
                frame.setSize (300, 300);
                frame.setVisible(true);
            }
        }

        class CursorBugPanel extends JPanel {
            // Constructor
            public CursorBugPanel () {

        // BUG: fails to set cursor for panel
                setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        //
                // Create a quit button
                JButton button = new JButton("Quit");
                button.addActionListener (new ActionListener () {
                    public void actionPerformed (ActionEvent e) {
                        System.exit (0);
                    }
                });
        // Sets cursor for button, no problemmyjava

        button.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
        //
                add(button);
            }
        }
        (Review ID: 25216)
        ======================================================================

        Another test case - Cursors work with Panels but not with JPanels.
        brian.klock@eng 1998-07-31

        //------------------------------CursorBug2.java-----------------------------
        import java.awt.*;
        import com.sun.java.swing.*;

        public class CursorBug2
        {
            static public void main(String args[])
            {
                JFrame frame1 = new JFrame("Crosshair Cursor shows up with a Panel");
                frame1.setSize(500, 200);
                frame1.setLocation(50, 20);
         
                JButton button = new JButton("PressMe");
                button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

                Panel panel = new Panel();
                panel.setBackground(Color.blue);
                panel.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                panel.add(button);

                frame1.getContentPane().add("Center", panel);
                frame1.setVisible(true);


                JFrame frame2 = new JFrame("But not with a JPanel");
                frame2.setSize(500, 200);
                frame2.setLocation(600, 20);
         
                JButton button2 = new JButton("PressMe");
                button2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

                JPanel panel2 = new JPanel();
                panel2.setBackground(Color.blue);
                panel2.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                panel2.add(button2);

                frame2.getContentPane().add("Center", panel2);
                frame2.setVisible(true);
            }
        }

        ====================================================================

        This next test case shows that just a plain vanilla JComponent can't have Cursors either, while JButtons can. The left side component (a JComponent) should have the MOVE Cursor.


        //--------------------------CursorBug3.java-------------------------------
        import java.awt.*;
        import com.sun.java.swing.*;
        import com.sun.java.swing.border.*;

        public class CursorBug3
        {
            static public void main(String args[])
            {
                JFrame frame = new JFrame();
                frame.setSize( new Dimension( 200, 200));
          
                JSplitPane j = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
                ExtJComponent pane = new ExtJComponent();

                JButton button1 = new JButton("PressMe");
                button1.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

                j.setLeftComponent(pane);

                j.setRightComponent(button1);
                j.setContinuousLayout(true);
                frame.getContentPane().add("Center", j);
                //frame.getContentPane().add("East", pane);
                frame.setVisible(true);
                pane.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                System.out.println(pane.getSize());
            }
        }

        class ExtJComponent extends JComponent {
            public ExtJComponent() {
                super();
                setOpaque(true);
                setBackground(Color.green);
                setForeground(Color.red);
                setBorder(new BevelBorder(BevelBorder.RAISED));
            }
            public void paintComponent(Graphics g) {
                g.drawString("Hello World", 20, 30);
            }
            public Dimension getPreferredSize() {
                return new Dimension(100,100);
            }
        }

              xdengsunw Xianfa Deng (Inactive)
              rmandelsunw Ronan Mandel (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: