Kermit
Kermit

Reputation: 34055

Temporarily disable MouseListener

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

Upvotes: 3

Views: 16353

Answers (2)

vishal_aim
vishal_aim

Reputation: 7854

You can also use glass pane to block user interaction

For example: block events

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285402

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.

Upvotes: 6

Related Questions