Results 1 to 3 of 3
  1. #1
    jackal is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default JFrame: JButtons in a grid

    Hi, I am trying to make a checkers game using a JFrame. So far I was able to display each space on the checker board as a JButton. But when I try to remove all the buttons from the grid and put new ones in by using the removeAll method it only displays a transparent JFrame.

    Here is my code:

    Java Code:
    public class BoardOperations 
    {
    	
    	public static void initializeBoard(JFrame checkerBoard, ImageIcon blkBackground, ImageIcon redBackground, ImageIcon whiteChkr, ImageIcon redChkr, boolean chkrBlockWhite, boolean chkrBlockRed)
    	{
    		checkerBoard.setSize(1000,1000);
    		checkerBoard.setTitle("CheckerBoard");
    		checkerBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            int row = 8;
     
            int col = 8;
     
            Container pane = checkerBoard.getContentPane();
            pane.setLayout(new GridLayout(row,col, 1, 1));
            
            int count = 0;
             
            //Populates the board with spaces and checkers
            for (int x = 1; x <=(row*col); x++)
            {
               int altr = 0;
               altr = (x-1) % col;
               altr += (x-1) / col;
               //Inserts red spaces in checker board
               if (altr % 2 == 0)
               {
                  pane.add(new JButton(redBackground));
               }
               //Inserts black spaces or checkers in checker board	
               else
               {
                	//Inserts white checkers
                	if(chkrBlockWhite == true && count < 12)
             	   		pane.add(new JButton(whiteChkr));
                	
                	//Inserts red checkers
             	    else if(chkrBlockRed == true && count > 19)
             	   		pane.add(new JButton(redChkr));
             	    //Inserts black spaces
             	    else
                       pane.add(new JButton(blkBackground));
                	count++;
                   
                }
     
              
               
                
            }
           
            checkerBoard.setVisible(true);
            
        }
    	public static void movePiece(JFrame checkerBoard, ImageIcon blkBackground, ImageIcon redBackground, ImageIcon whiteChkr, ImageIcon redChkr, boolean chkrBlockWhite, boolean chkrBlockRed)
    	{
    		checkerBoard.removeAll();
    		
    		Container pane = checkerBoard.getContentPane();
            pane.setLayout(new GridLayout(8,8, 1, 1));
    		for (int x = 1; x <=(8*8); x++)
            {
               int altr = 0;
               altr = (x-1) % 8;
               altr += (x-1) / 8;
               //Inserts red spaces in checker board
               if (altr % 2 == 0)
               {
                  pane.add(new JButton(redBackground));
               }
               //Inserts black spaces or checkers in checker board	
               else
               {
                	//Inserts white checkers
                	if(chkrBlockWhite == true)
             	   		pane.add(new JButton(whiteChkr));
                	
                	//Inserts red checkers
             	    else if(chkrBlockRed == true)
             	   		pane.add(new JButton(redChkr));
             	    //Inserts black spaces
             	    else{
                       pane.add(new JButton(blkBackground));
                	   
             	    }
                   
                }
             }
    		
    	}
    }

  2. #2
    jackal is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    Never mind I figured out my problem, I was using removeAll() on the JFrame when I should have been using it on the Container.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Generally you need to tell the Container that you have finished adding/removing components from it so all the components can be layed out properly. One way to do this is to use:

    validate();
    repaint();

    on the content pane.

Similar Threads

  1. How do you draw a grid in a JFrame
    By Singing Boyo in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 02:31 AM
  2. Replies: 0
    Last Post: 03-10-2009, 09:55 AM
  3. Replies: 5
    Last Post: 03-09-2009, 03:09 AM
  4. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 09:38 AM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •