Search code examples
javaarraysswingjframejbutton

Add Same Array Of Buttons To Two JFrame's


I am trying to add a array of buttons to two JFrame's to form a grid but when I try to do so the first JFrame has no buttons but the second time that i add the Buttons all the buttons are there.

enter image description here

My code is

public class GUI
{
    ReversiButton[][] reversi = new ReversiButton[8][8];
    JFrame WhiteFrame = new JFrame();
    JFrame BlackFrame = new JFrame();
    JLabel WhiteLabel = new JLabel();
    JLabel BlackLabel = new JLabel();
    JPanel BlackGrid = new JPanel();
    JPanel WhiteGrid = new JPanel();
    JButton WhiteButton = new JButton();
    JButton BlackButton = new JButton();
    public GUI()
    {
        populateArray();
        initGUI();
    }
    private void populateArray()
    {
        for(int y = 0;y<8;y++)
        {
            for(int x = 0; x<8;x++)
            {
                reversi[x][y] = new ReversiButton();
            }
        }
    }
    private void initGUI()
    {
        WhiteFrame.setTitle("Reversi White Player");
        BlackFrame.setTitle("Reversi Black Player");
        WhiteFrame.setLayout(new BorderLayout());
        WhiteLabel.setText("White Player - click place to put piece");
        WhiteGrid.setLayout(new GridLayout(8,8));
        for(int wy = 0;wy<8;wy++)
        {
            for(int wx = 0; wx<8;wx++)
            {
                WhiteGrid.add(reversi[wx][wy]);
            }
        }
        WhiteButton.setText("Greedy AI(play white)");
        WhiteFrame.add(BorderLayout.NORTH,WhiteLabel);
        WhiteFrame.add(BorderLayout.CENTER,WhiteGrid);
        WhiteFrame.add(BorderLayout.SOUTH,WhiteButton);
        WhiteFrame.pack();
        WhiteFrame.setVisible(true);
        BlackFrame.setLayout(new BorderLayout());
        BlackLabel.setText("Black player - not your turn");
        BlackGrid.setLayout(new GridLayout(8,8));
        for(int y = 0; y<8; y++)
        {
            for(int x = 0; x<8; x++)
            {
                BlackGrid.add(reversi[x][y]);
            }
        }
        BlackButton.setText("Greedy AI(play black)");
        BlackFrame.add(BorderLayout.NORTH, BlackLabel);
        BlackFrame.add(BorderLayout.CENTER, BlackGrid);
        BlackFrame.add(BorderLayout.SOUTH,BlackButton);
        BlackFrame.pack();
        BlackFrame.setVisible(true);
    }
}

How can I show the same array in two different JFrames?


Solution

  • From this Java Tutorial:

    Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.

    Because of that, to solve your problem you need to create two seperate button arrays; keeping in mind that now when one player makes a move, you will have to apply it to both grids:

    private static final int GRID_WIDTH = 8;
    private static final int GRID_HEIGHT = 8;
    
    JFrame whiteFrame = new JFrame();
    JFrame blackFrame = new JFrame();
    
    JPanel blackGrid = new JPanel();
    JPanel whiteGrid = new JPanel();
    
    JButton[][] whiteTiles = new JButton[GRID_WIDTH][GRID_HEIGHT];
    JButton[][] blackTiles = new JButton[GRID_WIDTH][GRID_HEIGHT];
    JButton whiteAIButton = new JButton();
    JButton blackAIButton = new JButton();
    
    JLabel whiteLabel = new JLabel();
    JLabel blackLabel = new JLabel();
    
    public GUI() {
        populateArray(whiteTiles);
        populateArray(blackTiles);
    
        initGUI();
    }
    
    private void populateArray(JButton[][] btnArray) {
        for (int x = 0; x < btnArray.length; x++) {
            for (int y = 0; y < btnArray[0].length; y++) {
                btnArray[x][y] = new JButton();
            }
        }
    }
    
    private void initGUI() {
        whiteAIButton.setText("Greedy AI (play white)");
        whiteLabel.setText("White Player - click place to put piece");
        fillGrid(whiteGrid, whiteTiles);
    
        whiteFrame.setLayout(new BorderLayout());
        whiteFrame.setTitle("Reversi White Player");
        whiteFrame.add(BorderLayout.NORTH, whiteLabel);
        whiteFrame.add(BorderLayout.CENTER, whiteGrid);
        whiteFrame.add(BorderLayout.SOUTH, whiteAIButton);
        whiteFrame.pack();
        whiteFrame.setVisible(true);
    
        blackAIButton.setText("Greedy AI (play black)");
        blackLabel.setText("Black player - not your turn");
        fillGrid(blackGrid, blackTiles);
    
        blackFrame.setTitle("Reversi Black Player");
        blackFrame.setLayout(new BorderLayout());
        blackFrame.add(BorderLayout.NORTH, BlackLabel);
        blackFrame.add(BorderLayout.CENTER, blackGrid);
        blackFrame.add(BorderLayout.SOUTH, blackAIButton);
        blackFrame.pack();
        blackFrame.setVisible(true);
    }
    
    private void fillGrid(JPanel grid, JButton[][] tiles) {
        grid.setLayout(new GridLayout(GRID_WIDTH, GRID_HEIGHT));
    
        for (int x = 0; x < GRID_WIDTH; x++) {
            for (int y = 0; y < GRID_HEIGHT; y++) {
                grid.add(tiles[x][y]);
            }
        }
    }
    

    Note: I changed your ReversiButtons to simply JButtons so that the code would compile on its own.