1

The code below is supposed to create and object instance for a specific type (say color) JButton I want to represent in a grid. When I iterate through the for-loop to add the buttons to the jframe it adds nothing. But if I add a single instance variable it will add that. Anybody have an idea?

public class Grid {

protected JButton [][] board;

private JButton player;
private JButton openCell;
private JButton wall;
private JButton closedCell;

public Grid(String [] args) { // args unused
    // Instantiation 
    board = new JButton [6][6];
    layout = new String [6][6];

    blueCell = new JButton("BLUE CELL");
    redCell = new JButton("RED CELL");
    greenCell = new JButton("GREEN CELL");
    whiteCell = new JButton("WHITE CELL");

    // Configuration (add actions later)

    // Decoration
    blueCell.setBackground(Color.blue);
    redCell.setBackground(Color.red);
    greenCell.setBackground(Color.green);
    whiteCell.setBackground(Color.white);

    for (int rows = 0; rows < 6; rows++) {
        for (int cols = 0; cols < 6; cols++) {
            if ((layout[rows][cols]).equals('*')) {
                board[rows][cols] = blueCell;
            }
            else if ((layout[rows][cols]).equals('.')) {
                board[rows][cols] = redCell;
            }
            else if ((layout[rows][cols]).equals('x')) {
                board[rows][cols] = greenCell;
            }
            else {
                board[rows][cols] = whiteCell;
            }
        }
    }

    JFrame game = new JFrame();
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setLayout(new GridLayout (6, 6));
    game.setSize(500, 500);


    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board.length; j++) {
            if ((board[i][j]).equals(blueCell)) {
                grid.add(blueCell);
            }
            else if ((board[i][j]).equals(redCell)) {
                grid.add(redCell);
            }
            else if ((board[i][j]).equals(greenCell)) {
                grid.add(greenCell);
            }
                else {
                grid.add(whiteCell);
            }
        }
    }

    grid.setVisible(true);

} // end of constructor
} // end of Grid class
2
  • Please don't delete your code and question as it makes all answers to the original question obsolete, and means that I've wasted my time trying to help you. Please restore your prior code, and if you desire add new code as an edit to the question, as I've edited my answer. Commented Nov 23, 2013 at 21:50
  • I've rolled back your question. If you want to edit this and add info to the bottom, then please do so. Or if your new idea is completely different, then consider asking a completely new question on this site. Commented Nov 23, 2013 at 21:52

1 Answer 1

4

You can add a component to your GUI only once. If you add it to another component, it will be removed from the previous component. You're trying to add the same JButtons several times, and that won't work. Instead you're going to have to create more JButtons. Consider having your buttons share Actions which is allowed.

If you need more help, consider posting compilable code (your current code is not), a small runnable, compilable program that demonstrates your problem, in other words, an sscce.


Edit
You comment:

But don't these count as instances of a JButton not the same JButton? (I don't understand what your answer meant...)

Think of it mathematically... how many JButtons do you create in your code above? Well, this is easy to figure, exactly 4:

blueCell = new JButton("BLUE CELL");
redCell = new JButton("RED CELL");
greenCell = new JButton("GREEN CELL");
whiteCell = new JButton("WHITE CELL");

So, now ask yourself, how many JButtons are you trying to display in your GUI with these four JButtons? If it's four, then you're possibly OK (as long as you use each button), but if it's more, then you're in trouble. From your 6x6 grid, board = new JButton [6][6];, it looks like you're trying to display 36 JButtons, and if this is true, you've got problems.

But again, if still stuck, please consider creating and posting an sscce.

Sign up to request clarification or add additional context in comments.

3 Comments

But don't these count as instances of a JButton not the same JButton? (I don't understand what your answer meant...)
Thank you I'll try... Would this.<cell> fix the problem? It could account for many instances, no?
@user2449907: I don't know what you mean by this.<cell>. The bottom line though is you need a unique JButton for each one you want to display. Period.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.