Thomas Gaillot
Thomas Gaillot

Reputation: 23

Can I change an unique MouseListener methods with several JButton in JAVA?

Goal

What I want to do is to set a MouseListener on a Panel when click on a Button1. Then I want to click on another Button that changes the MouseListener code to do something else.

Example of application

  1. Click on JButton1 -> add MouseListener that change a JLabel background color to red.
  2. Click on JButton2 -> DOESNT ADD a new MouseListener, but change the first one to set the JLabel text to "hello world"

What I can't do

I don't know how to modify an UNIQUE MouseListener.

What I tried

I tried to set an jButton.actionPerformed( new jLabel1.addMouseListener()) for each button, but they create two instance of MouseListener.

I don't want to set one MouseListener for several JButtons, but several JButton changing the status of my MouseListener.

Thanks alot :)

Upvotes: 2

Views: 244

Answers (1)

Better to give the JLabel a MouseListener from the get-go, but give it boolean if-blocks that will turn on or off functionality depending on the state of class boolean fields. In your button ActionListeners, simply change the state of these boolean fields. For example in the code below, the boolean flag labelListenerOn is toggled on or off in the first JButton's ActionListener. The JLabel's MouseListener checks the state of this variable and changes the labels background color if the flag is true only. Similarly for the other boolean flag and other ActionListener:

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class ButtonListener extends JPanel {
    private static final String TURN_ON_MOUSE = "Turn On Mouse";
    private static final String TURN_OFF_MOUSE = "Turn Off Mouse";
    private JButton button1 = new JButton(TURN_ON_MOUSE);
    private JButton button2 = new JButton("Button 2");
    private JLabel label1 = new JLabel("Label 1");
    private MouseListener labelListener = new LabelListener();
    private boolean labelListenerOn = false;
    private boolean labelChangeText = false;

    public ButtonListener() {
        label1.setOpaque(true);
        label1.addMouseListener(labelListener);

        button1.addActionListener(e -> {
            if (labelListenerOn) {
                labelListenerOn = false;
                ((JButton) e.getSource()).setText(TURN_ON_MOUSE);
            } else {
                labelListenerOn = true;
                ((JButton) e.getSource()).setText(TURN_OFF_MOUSE);
            }

        });
        
        button2.addActionListener(e -> {
            labelChangeText = true;
        });

        add(button1);
        add(button2);
        add(label1);
    }

    private class LabelListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            Color labelColor = label1.getBackground();
            if (labelListenerOn) {
                if (labelColor.equals(Color.RED)) {
                    label1.setBackground(null);
                } else {
                    label1.setBackground(Color.RED);
                }
                // label1.repaint(); // per Rob Camick's comment, this is not necessary
            }
            
            if (labelChangeText) {
                label1.setText("Hello World");
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        ButtonListener mainPanel = new ButtonListener();
        JFrame frame = new JFrame("ButtonListener");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

If you want to get fancy, look up M-V-C for "model-view-controller", where you separate the program logic (here the state of the boolean flags) from the program view code (the Swing GUI code), usually in their own classes, and then use a master class to hook all the components up. This would add an additional layer of indirection and complexity, and would be over-kill in this situation, but in large programs, and especially in programs that are likely going to be updated, changed and grown, this will actually reduce complexity in the long run, and make the program much more "scalable" -- easier to grow and modify. Again, I do not recommend that you do this here, but do look it over for possible future use.

Upvotes: 3

Related Questions