Results 1 to 8 of 8
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    0

    Default Confusion regarding changing mouse cursors explicitly

    I have written a Swing program for CAD purposes which lets you drop blocks and connect them together, move them, etc.

    Right now I've implemented a "Delete" mode that is initiated from the Menu. That Menu event handler sets the mode variable and turns the Mouse cursor to a crosshair. This event is handled in the Frame class.

    The next time you click on a block in the right place (MousePressed event), the block is deleted and the mode is reset, and the mouse cursor is reset to the default arrow. This event is handled in the Panel class that is part of the frame.

    Well that's great, except when the mouse goes back over the menu, it turns to the cross hair! If you select "Delete" again from the menu, the cursor does not change when in the main JPanel, but delete mode is still viable there.

    Both of these events which change the mouse cursor are event handlers and so they shouldn't have a problem with needing to be wrapped with InvokeLater(). Nevertheless I tried that and it didn't change anything.

    If nobody has the answer off the top of their head I'll whittle it down to a SSCCE.

    Thanks,

    DL

  2. #2
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    0

    Default Trouble controlling mouse cursor explicitly

    I change the mouse cursor to the crosshair from the "Edit-Delete" menu item in the SpinFrame class.

    I try changing it back using a MousePressed event handler in the JPanel that is in the SpinFrame.

    It works, but the cursor is still the crosshair when it's not over the JPanel. Then you try again and you can't make it be the crosshair over the JPanel ever again.

    Java Code:
    package painting;
    
    import java.awt.BorderLayout;
    // import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JSlider;
    import javax.swing.JToolBar;
    import javax.swing.JButton;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JMenu;
    import javax.swing.SwingUtilities;
    
    import java.awt.event.ActionEvent;
    
    import java.awt.event.ActionListener;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.SystemColor;
    
    public class SpinFrame extends JFrame {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JPanel contentPane;
    	private final simControlToolBar sctb = new simControlToolBar();
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					SpinFrame dspFrame = new SpinFrame();
    					dspFrame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public SpinFrame() {
    		// panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
    		setTitle("Java Forums");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 800, 600);
    
    		final SpinPanel panel = new SpinPanel(this);
    		panel.setBackground(SystemColor.inactiveCaption);
    		// ==========================================================================================
    		// ======================= main panel =========================================================
    
    		contentPane = new JPanel();
    		contentPane.setBackground(Color.LIGHT_GRAY);
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    
    		contentPane.add(panel, BorderLayout.CENTER);
    
    		// ==========================================================================================
    		// ======================= toolbars =========================================================
    
    		sctb.setFloatable(false);
    		contentPane.add(sctb, BorderLayout.NORTH);
    
    		JMenuBar menuBar = new JMenuBar();
    		setJMenuBar(menuBar);
    
    		JMenu mnEdit = new JMenu("Edit");
    		menuBar.add(mnEdit);
    
    		JMenuItem mntmDelete = new JMenuItem("Delete");
    		mntmDelete.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				Cursor deleteCursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
    				setCursor(deleteCursor);
    			}
    		});
    		mnEdit.add(mntmDelete);
    	}
    
    	//======================================================================================================
    	class simControlToolBar extends JToolBar {
    
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = -4298199583629847984L;
    		final JButton btnStartSimulation = new JButton("Start Simulation");
    		final JButton btnSigGen= new JButton("Sig Gen Sim");
    
    		final JSlider pot0Slider = new JSlider(0,100,1);
    		final JSlider pot1Slider = new JSlider(0,100,1);
    		final JSlider pot2Slider = new JSlider(0,100,1);
    
    		public simControlToolBar() {
    			super();
    			this.add(btnStartSimulation);
    			this.add(btnSigGen);
    			this.add(pot0Slider);
    			this.add(pot1Slider);
    			this.add(pot2Slider);
    		}
    	}
    }
    Java Code:
    package painting;
    
    import java.awt.Cursor;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JPanel;
    // =======================================================================================================
    public class SpinPanel extends JPanel {
    
    	private static final long serialVersionUID = 1L;
    	/*	
    	public SpinCADPanel () {
    
    	}
    	 */
    	public SpinPanel (final SpinFrame spdFrame) {
    		addMouseListener(new MouseAdapter() {
    			@Override
    			public void mousePressed(MouseEvent arg0) {
    				Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
    				setCursor(defaultCursor);
    				return;
    			}
    		});  
    	}
    	//=======================================================================================================
    }

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: Trouble controlling mouse cursor explicitly

    You might want to do this in a MouseListener on the JMenuItem: change it to crosshair in mouseEntered(MouseEvent e) and back to default in mouseExited(MouseEvent e).

  4. #4
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    0

    Default Re: Trouble controlling mouse cursor explicitly

    Quote Originally Posted by SurfMan View Post
    You might want to do this in a MouseListener on the JMenuItem: change it to crosshair in mouseEntered(MouseEvent e) and back to default in mouseExited(MouseEvent e).
    Hmmm I'm not sure I understand exactly... The mouse pointer should always be the default when it's navigating the menu, and it should be default or crossbar in the JPanel depending on whether the user previously selected the "Delete" menu item. The biggest problem is that when over the menu, it changes back to crosshair even after I've (I thought) turned it back to default (and it does return to default, but only over the JPanel).

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Confusion regarding changing mouse cursors explicitly

    I think we're going to need that SSCCE.

    Good call on trying wrapping in invokeLater(...), pending Swing code can sometimes reset settings made by your code.

    Note that setCursor(...)
    causes that cursor to be displayed within all of the container's subcomponents, except for those that have a non-null cursor.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Confusion regarding changing mouse cursors explicitly

    I've merged two threads here. Please don't double post again. Note that double posting the same question is a violation of the second paragraph of the Forum Rules.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    0

    Default Re: Confusion regarding changing mouse cursors explicitly

    Sorry about double posting!!!!

    Well OK, setting the mouse cursor within the JFrame would also change it within the JPanel which is contained within the JFrame. However changing it back from the JPanel will only change it in the JPanel. Seems like this is what's happening, at least partially. So it looks like I'll need to send a message from the JPanel to the JFrame to change the cursor back to normal, or (maybe even better) initially send a message from the Menu event handler in the JFrame down to the Jpanel to change it to a crosshair. It really only needs to be a crosshair in the JPanel and it doesn't make sense for it to be that way over the JSLiders in the JToolbar or over the JMenu.

    Let me try some things and I'll get back!

    Thanks,

    DL
    Last edited by Digital Larry; 05-25-2013 at 07:58 PM.

  8. #8
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    0

    Default Re: Confusion regarding changing mouse cursors explicitly

    Well that was way easier than I imagined it would be. I was already making a call into the JPanel to set the edit mode from the JMenu handler (wasn't in the SSCCE). I just moved the mouse cursor changing code to there from the JFrame menu event handler.

    THANK YOU!!!!!!!!!!!!!

Similar Threads

  1. Changing the Mouse Cursor
    By CuppaCoffee in forum AWT / Swing
    Replies: 2
    Last Post: 12-22-2012, 10:41 PM
  2. Replies: 9
    Last Post: 07-17-2011, 09:24 AM
  3. ORA-01000 maximum open cursors exceeded
    By jeev81 in forum Advanced Java
    Replies: 4
    Last Post: 12-14-2010, 01:10 PM
  4. Java Cursors
    By 67726e in forum Advanced Java
    Replies: 20
    Last Post: 09-29-2010, 04:09 AM
  5. [SOLVED] Custom Cursors: Thick Lines
    By Paul Richards in forum Advanced Java
    Replies: 4
    Last Post: 06-03-2009, 09:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •