Temporarily disable MouseListener

14,574

Solution 1

Without looking at your code, I would simply have my MouseListener's behavior depend on its state. I would give it a state boolean variable, say called enabled, complete with getters and setters, and then short-circuit the code if enabled is false. i.e., the specific methods could look something like:

public void mousePressed(MouseEvent mEvt) {
  if (!enabled) {
    return;
  }
  // rest of mousePressed goes here
}

Another suggestion, don't do this:

public class P2 extends JPanel implements MouseListener {

Don't have your GUI classes implement your listener interfaces as you are asking the class to do too much. This is OK for toy programs or very small demo programs, but for larger projects, you will want to separate out your logic from your view from your control.

Solution 2

You can also use glass pane to block user interaction

For example: block events

Share:
14,574

Related videos on Youtube

Kermit
Author by

Kermit

Writing a question? Follow Jon Skeet's guide on how to write a good question. Answering a question? Follow Jon Skeet's guide on how to write a good answer.

Updated on September 26, 2022

Comments

  • Kermit
    Kermit over 1 year

    I'm working on assignment with two JPanels. One panel contains a moving ball which moves by default and the other panel has two JRadioButtons that are labeled On and Off. The part that I'm stuck on is disabling and enabling the MouseListener (P2.java) that allows the user to click on the panel to reposition the ball. I've created functions turnOn and turnOff that are triggered using an ActionListener (P1.java). This starts and stops the ball. I've tried to use removeActionListener, however the compiler throws the error that I'm not able to use the method.

    In addition, would be easier to use an ItemListener like in this example so that when the JRadioButton is already selected it is ignored?

    P1.java

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class P1 extends JPanel
    {
        private JRadioButton on = new JRadioButton("On", true);
        private JRadioButton off = new JRadioButton("Off");
    
        public P1()
        {
            ButtonGroup group = new ButtonGroup();
            group.add(on);
            group.add(off);
    
            add(on);
            add(off);
    
            ButtonHandler bh = new ButtonHandler();
            on.addActionListener(bh);
            off.addActionListener(bh);
        }
    
        private class ButtonHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                if(ae.getSource() == on) turnOn();
    
                if(ae.getSource() == off) turnOff();
            }
        }
    
        public static void turnOn () {
            Ball.dx = 1;
            Ball.dy = 1;
        }
    
        public static void turnOff () {
            Ball.dx = 0;
            Ball.dy = 0;
        }
    }
    

    P2.java

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class P2 extends JPanel implements MouseListener
    {
        public P2()
        {
            super();
            addMouseListener(this);
        }
    
        public void mousePressed(MouseEvent e)
        {
            ball.x = e.getX();
            ball.y = e.getY();
            repaint();
        }
    
        ...
    }
    

    Rest of the project

    • Hovercraft Full Of Eels
      Hovercraft Full Of Eels about 11 years
      Please don't post links to code. Remember we're volunteers, and if the code is too large to post here, it's likely too large to ask volunteers to go through. Also you'll want to make it as easy as possible for others to help you. Instead post the code itself here, preferably distilled down to only that necessary to allow it to compile and run and show your problem, an sscce. Yes, this will require quite a bit of effort on your part, but it will be effort that helps us help you and it won't be wasted.
    • MadProgrammer
      MadProgrammer about 11 years
      I'm only guessing, but of site linked code tends to be frowned upon. If you have the functionality to stop/start the ball, why not just raise a flag to indicate to the listener that it should ignore mouse events, or simply remove the mouse listener...
  • Stijn de Witt
    Stijn de Witt over 8 years
    Great answer, but I disagree with "Don't have your GUI classes implement your listener interfaces". To me the GUI components seem the best place to handle GUI events like mouse listeners. The handling code should however be just a couple of lines of code dealing with the GUI logic of the handler, then delegating to a business method/event listener that deals with the business logic of the event.