JPopupMenue and buttons

264 views Asked by At

I want that my program will do the following:

for every button to add jpopupmenue-that work when click on one of the buttons to show its jpopupmenue and when:

  • the first JMenueItem was clicked- to change the color of the button that trigger the jpopupmenue to red;
  • the second JMenueItem was clicked- to change the color of the button that trigger the jpopupmenue to blue;
  • the third JMenueItem was clicked- to change the color of the button that trigger the jpopupmenue to yellow, and only it.

The problem is that when I click on one of the buttons and change its color by the jpopupmenue then the second button that is clicked is automatically changed to the same color....

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 
 * @author shani moyal
 */
public class systemgraph extends JPanel implements ActionListener {

    // data//

    private int countplugs;
    JPopupMenu popup;
    private JButton btn;
    static JFrame frame;
    private JButton[] buttons;
    private BufferedImage bi;
    JMenuItem m2Frame1;
    JMenuItem m2Frame3;
    JMenuItem m2Frame2;

    public systemgraph(int size) {
        popup = new JPopupMenu();
        m2Frame1 = new JMenuItem("plug");
        m2Frame2 = new JMenuItem("second");
        m2Frame3 = new JMenuItem("third");
        popup.add(m2Frame1);
        popup.add(m2Frame2);
        popup.add(m2Frame3);
        popup.addMouseListener(null);

        buttons = new JButton[size];

        for (int i = 0; i < size; i++) {
            ImageIcon circle = new ImageIcon(
                    "C:\\Users\\shani moyal\\Desktop\\תמונות לגרפיקה         לפרויקט   \\imagesCA4ZS816.jpg");
            buttons[i] = new JButton((String.valueOf(i)));
            buttons[i].setName(String.valueOf(i));
            buttons[i].setBackground(Color.GREEN);
            buttons[i].setPreferredSize(new Dimension(80, 80));
            buttons[i].setEnabled(true);
            buttons[i].addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    // JButton button = (JButton)e.getSource();

                    final Component cmp = (Component) e.getSource();

                    popup.show(e.getComponent(), e.getX(), e.getY());
                    // popup.addMouseListener(this);

                    m2Frame1.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mousePressed(MouseEvent e) {
                            cmp.setBackground(Color.red);

                        }
                    });

                    m2Frame2.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mousePressed(MouseEvent e) {
                            cmp.setBackground(Color.BLUE);

                        }
                    });
                    m2Frame3.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mousePressed(MouseEvent e) {
                            cmp.setBackground(Color.YELLOW);

                        }
                    });

                    /*
                     * m2Frame1.addMouseListener(new MouseAdapter() {
                     * 
                     * @Override public void mousePressed(MouseEvent e) {
                     * cmp.setBackground(Color.MAGENTA);
                     * 
                     * } });
                     */

                }
            });

            this.add(buttons[i], BorderLayout.NORTH);

        }

    }

    private static void createAndShowGUI() {

        frame = new JFrame("ButtonDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Simple Fiber GUI system (K&CG lab. Ariel U.");
        systemgraph newContentPane = new systemgraph(6);
        newContentPane.setOpaque(true);

        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
2

There are 2 answers

3
DWilches On BEST ANSWER

When you press a button for the first time you are adding a MouseListener to m2Frame1. When you press another button you are adding another MouseListener, but note that you're not removing the first one. So when you click on the JMenuItem m2Frame1 you will call both MouseListeners.

What is happening here is that as 2 Mouselisteners are being called, you are getting the color of 2 buttons changed.

You should try to add just one MouseListener to m2Frame1 at the beginning of the code. Of course yo'll need to figure out how can you make the JMenuItem choose which button it must modify.

0
Pshemo On

Maybe create your own class that will extend JPopupMenu and will contain field that will be used to determine which button need to change. This class can look like

class MyPopupMenu extends JPopupMenu{

    private static final long serialVersionUID = 1L;

    private JButton button;

    public JButton getButton() {
        return button;
    }

    public void setButton(JButton button) {
        this.button = button;
    }
}

You will need to change JPopupMenu popup to MyPopupMenu popup and code inside constructor in your SystemGraph class can look like

public SystemGraph(int size) {
    popup = new MyPopupMenu();
    m2Frame1 = new JMenuItem("red");
    m2Frame2 = new JMenuItem("blue");
    m2Frame3 = new JMenuItem("yellow");
    popup.add(m2Frame1);
    popup.add(m2Frame2);
    popup.add(m2Frame3);
    popup.addMouseListener(null);

    buttons = new JButton[size];

    for (int i = 0; i < size; i++) {
        ImageIcon circle = new ImageIcon(
                "C:\\Users\\shani moyal\\Desktop\\תמונות לגרפיקה         לפרויקט   \\imagesCA4ZS816.jpg");
        buttons[i] = new JButton((String.valueOf(i)));
        buttons[i].setName(String.valueOf(i));
        buttons[i].setBackground(Color.GREEN);
        buttons[i].setPreferredSize(new Dimension(80, 80));
        buttons[i].setEnabled(true);
        final JButton tmp = buttons[i];
        buttons[i].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                popup.setButton(tmp);
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        });

        this.add(buttons[i], BorderLayout.NORTH);

    }

    m2Frame1.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            MyPopupMenu p = (MyPopupMenu)e.getComponent().getParent();
            p.getButton().setBackground(Color.RED);
        }
    });

    m2Frame2.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            MyPopupMenu p = (MyPopupMenu)e.getComponent().getParent();
            p.getButton().setBackground(Color.BLUE);
        }
    });
    m2Frame3.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            MyPopupMenu p = (MyPopupMenu)e.getComponent().getParent();
            p.getButton().setBackground(Color.YELLOW);
        }
    });

}