0

I am trying to use the grid[x][y].setPressedBackgroundColor(getColor()); to set the background but I recieve this error message

error: cannot find symbol
 grid[x][y].setPressedBackgroundColor(getColor());
           ^
  symbol:   method setPressedBackgroundColor(Color)
  location: class JButton
1 error

Process completed.

Here is the code for the JButton. The error is thrown in the ButtonGrid constructor:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ButtonGrid {

 class MyButton extends JButton {

        public Color hoverBackgroundColor;
        public Color pressedBackgroundColor;

        public MyButton() {
            this(null);
        }

        public MyButton(String text) {
            super("text");
            super.setContentAreaFilled(false);
        }
        @Override
        public void paintComponent(Graphics g) {
            if (getModel().isPressed()) {
                g.setColor(pressedBackgroundColor);
            } else if (getModel().isRollover()) {
                g.setColor(hoverBackgroundColor);
            } else {
                g.setColor(getBackground());
            }
            g.fillRect(0, 0, getWidth(), getHeight());
            super.paintComponent(g);
        }

         @Override
        public void setContentAreaFilled(boolean b) {
        }

        public Color getHoverBackgroundColor() {
            return hoverBackgroundColor;
        }

        public void setHoverBackgroundColor(Color hoverBackgroundColor) {
            this.hoverBackgroundColor = hoverBackgroundColor;
        }

        public Color getPressedBackgroundColor() {
            return pressedBackgroundColor;
        }

        public void setPressedBackgroundColor(Color pressedBackgroundColor) {
            this.pressedBackgroundColor = pressedBackgroundColor;
        }
    }




        JFrame frame=new JFrame(); //creates frame
        JButton[][] grid; //names the grid

        public ButtonGrid(int width, int length){
             int count = 1; //counting variable
             frame.setLayout(new GridLayout(width,length)); //sets the layout
             grid=new JButton[width][length]; //sets the size of grid
             for(int y=0; y<length; y++){
                 for(int x=0; x<width; x++){
                     grid[x][y]=new JButton("( "+ count++ +" )"); //creates new button    
                     grid[x][y].setForeground(getColor()); //sets text color
                     grid[x][y].setHorizontalTextPosition(SwingConstants.CENTER); //centers text
                     grid[x][y].setBorder(null); //removes border
                     grid[x][y].setBackground(getColor()); //sets color of jbutton
                     grid[x][y].setPressedBackgroundColor(getColor());
                     frame.add(grid[x][y]); //adds button to grid                       
                 }
            }
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack(); //sets appropriate size for frame
            frame.setVisible(true); //makes frame visible
        }

       public Color getColor() {
           int rval = (int)Math.floor(Math.random() * 256);
           int gval = (int)Math.floor(Math.random() * 256);
           int bval = (int)Math.floor(Math.random() * 256);
           return new Color(rval, gval, bval);
       } 

       public static void main(String[] args) {
            new ButtonGrid(3,3);//makes new ButtonGrid with 2 parameters                    
       }
}
mdewitt
  • 2,526
  • 19
  • 23
  • I would probably do it using images, as shown in [this answer](http://stackoverflow.com/a/21248363/418556). – Andrew Thompson Jan 21 '14 at 18:46
  • BTW - Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Jan 21 '14 at 18:47
  • It should not be JButton[][] grid; //names the grid, but it should be MyButton[][] grid; You have overriden JButton class. – KernelPanic Jan 21 '14 at 18:47

1 Answers1

2

Change JButton[][] to MyButton[][]

Dodd10x
  • 3,344
  • 1
  • 18
  • 27