How do you terminate an ActionListener from memories of previous events

I am only new enough to java. I am tring to fill a 6x6 grid with 6 differnet colors without the same color appearing in the same row or column. in my code i have set a 6x6 grid of JButtons stored in an array with buttons. when i click one of these JButtons the 6x1 grid of JButtons is set up called paintBox. The JButtons in paintBox are declared at the top of the program as fillRed, fillYellow, etc, when I press fillRed it sets the back of the JButton from the 6x6 grid, but when I press another JButton from the 6x6 grid and try to set that for yellow it sets the color to yellow and also sets the original JButton, which was also set to red and yellow. any help would be great. thank

import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class Grid4 extends JFrame implements ActionListener
    {
        private ColourGrid paintBox = null;
        private JButton fillRed = new JButton("Red");
        private JButton fillYellow = new JButton("Yellow");
        private JButton fillBlue = new JButton("Blue");
        private JButton fillGreen = new JButton("Green");
        private JButton fillPurple = new JButton("Purple");
        private JButton fillBrown = new JButton("Brown");
        private JButton[] paintButton = {fillRed,fillYellow,fillBlue,fillGreen,fillPurple,fillBrown};
        private Color[] colours = {Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN, new Color(102, 0, 102), new Color(102, 51, 0)};
        public static void main(String[] args) // sets up a 6x6 grid
        {
            int rows = 6;
            int cols = 6;
            int size = 600;
            Grid4 grid = new Grid4(rows, cols);
            grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            grid.setPreferredSize(new Dimension(size, size));
            grid.pack();
            grid.setLocationRelativeTo(null);
            grid.setVisible(true);
        }
        // main
        public Grid4(int rows, int cols) // makes the 6x6 main grid a grid of JButtons
        {
            int rowSize = 6;
            int colSize = 6;
            int gridSize = 600;
            JButton[][] buttons; //makes an array called buttons
            buttons = new JButton[rowSize][colSize];
            Container pane = getContentPane();
            pane.setLayout(new GridLayout(rows, cols));
            for(int j =0; j < rows; j++){
                for (int i = 0; i < cols; i++) {
                    buttons[j][i] = new JButton("");
                    buttons[j][i].setOpaque(true);
                    buttons[j][i].setName("");
                    buttons[j][i].addActionListener(this);
                    buttons[j][i].setBackground(Color.BLACK);
                    pane.add(buttons[j][i]);
                }
            }
        }               //end of grid constructor

        public void actionPerformed(ActionEvent e) 
        {
            if ( paintBox != null && paintBox.isShowing())//stops more than one paintBox from opening
                paintBox.dispose();
            if( e.getSource() instanceof JButton){// sets
                ((JButton)e.getSource()).setBackground(Color.BLACK);
            } 

            int rows = 6;
            int cols = 1;
            int size = 300;
            paintBox = new ColourGrid(rows, cols,(JButton)e.getSource());
            paintBox.setPreferredSize(new Dimension(size/3, size));
            paintBox.pack();
            paintBox.setVisible(true);
        }

        public class ColourGrid extends JFrame
        { 
            private JButton buttonPress;

            public ColourGrid(int rows, int cols, JButton button)
            {

                buttonPress = button;
                Container pane = getContentPane();
                pane.setLayout(new GridLayout(rows, cols));
                for (int i = 0; i < paintButton.length; i++) {
                    paintButton[i].setOpaque(true);
                    paintButton[i].addActionListener(buttonAction);
                    paintButton[i].setForeground(new Color(100,100,100));
                    paintButton[i].setBackground(colours[i]);
                    pane.add(paintButton[i]);
                }
            }
            private ActionListener buttonAction = new ActionListener()
            {
            public void actionPerformed(ActionEvent a)
            {
                if(a.getSource() instanceof JButton){
                    if((JButton)a.getSource()== fillRed){
                    buttonPress.setBackground(Color.RED);
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillYellow){
                    buttonPress.setBackground(Color.YELLOW);
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillBlue){
                    buttonPress.setBackground(Color.BLUE);
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillGreen){
                    buttonPress.setBackground(Color.GREEN);
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillPurple){
                    buttonPress.setBackground(new Color(102, 0, 102));
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillBrown){
                    buttonPress.setBackground(new Color(102, 51, 0));
                    dispose();
                    }
                } 

            }
        };
        }
    }

      

+3


source to share


1 answer


Your problem is that your colored buttons are in the Grid4 class. Every time you create a new ColourGrid object, you add the same colored buttons to the new Jorramer ColourGrid and add an ActionListener to the same buttons. This way, every time this happens, the JButtons accumulates a different ActionListener, and pretty soon, whenever a color button is pressed, many ActionListeners fire both old and new ones, and all the buttons change color.

The solution is to make the colored buttons part of the ColourGrid class, not the Grid4 class:

public class ColourGrid extends JFrame {
  private JButton fillRed = new JButton("Red");
  private JButton fillYellow = new JButton("Yellow");
  private JButton fillBlue = new JButton("Blue");
  private JButton fillGreen = new JButton("Green");
  private JButton fillPurple = new JButton("Purple");
  private JButton fillBrown = new JButton("Brown");
  private JButton[] paintButton = { fillRed, fillYellow, fillBlue, fillGreen,
        fillPurple, fillBrown };
  private Color[] colours = { Color.RED, Color.YELLOW, Color.BLUE,
        Color.GREEN, new Color(102, 0, 102), new Color(102, 51, 0) };

  private JButton buttonPress;

      



This way, every time you create a new ColourGrid object, it gets fresh new JButtons, to which only one ActionListener is bound, and only the most recent color of the grid button changes.

Otherwise, all the recommendations that Andrey gave you are very good recommendations.

+5


source







All Articles