Search code examples
javaswingcursorjframe

Change cursor to hand over certain co-ordinates in a JFrame


I have a boolean that changes when the mouse is within the set co-ordinates inside the JFrame and then I have an if in my run method so that I can change the cursor but I cannot find anywhere of how to change the cursor without using a list or object. How would I change the cursor without using an object other than JFrame?


Solution

  • I'm not really sure why you would need to 'not' use other objects. If you were willing to use something like a JPanel, your code could become very simple. For example, you could add a JPanel to the area in question, and then set a cursor to recognize each time the cursor entered or left that swing component. For example;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class ExampleClass{    
    
        public static void main(String args[]){
    
            JFrame exampleFrame = new JFrame("Test");          
            exampleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            exampleFrame.setLayout(new FlowLayout());
            exampleFrame.setSize(300, 300);
    
            JPanel redPanel = new JPanel();
            redPanel.setBackground(Color.RED);
            redPanel.setPreferredSize(new Dimension(150, 300));
            redPanel.setCursor(new Cursor(Cursor.HAND_CURSOR)); // This one line changes the cursor.
    
            exampleFrame.add(redPanel);
            exampleFrame.setVisible(true);            
        }
    }
    

    Although it is entirely possible to do what your asking, it sounds as though it would be much more verbose/less readable than is necessary to see results. The JPanel wouldn't even need to be a different color, it could be completely invisible to the user (redPanel.setOpaque(false); would do the trick).

    Running the example code above shows a JFrame with a centralized red square (the JPanel redPanel). Moving your mouse over the red square changes the cursor.


    If for some reason the above method is impractical or your program design is very much set in it's ways, you could use absolute co-ordinates relative to your JFrame. Create a MouseMotionListener for your JFrame, like so;

    class CursorMouseMotionListener implements MouseMotionListener{
    
        JFrame yourFrame;
    
        CursorMouseMotionListener(JFrame yourFrame){
            this.yourFrame = yourFrame;
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            // Do nothing.
        }
        /// The important bit below
        @Override
        public void mouseMoved(MouseEvent e) {
            if(e.getPoint().x >= 50 && e.getPoint().x <=150 && e.getPoint().y >= 50 && e.getPoint().y <=150){
                yourFrame.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            else{
                yourFrame.setCursor(new Cursor(Cursor.MOVE_CURSOR));
            }
        }
    
    }
    

    Then add this to your JFrame like so;

    frame.addMouseMotionListener(new CursorMouseMotionListener(frame));
    

    You will have to change the coordinates, of course, to match the position of your buttons but then your good. I still wouldn't advise this method if you can avoid it. Better to use the swing components at your disposal. But if you absolutely need to with your current program then this should work.