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) {
}
}
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.