Results 1 to 2 of 2
  1. #1
    stevemcc is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    Default 2D Array of JButtons

    I must write a connect4 gui. I started off by creating a 2D array of JButtons. It well recieved by the java compiler though.
    Java Code:
    import java.awt.*;
    
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    
    public class AppPanel extends JPanel {
    
    
    
    	private JButton[][] slots = new JButton[6][7];
    
    		
    
    	public AppPanel() {
    
    		
    
    		for(int i=0; i<6; i++) {
    
    			for(int x=0; x<7; x++) {
    
    
    				add(slots[i][x]);
    
    			}
    
    		}
    
    	}
    }
    I really cannot figure what is wrong with it. Can anyone offer a suggestion?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    // This only/merely allocates an array of JButtons.
    // Each element of the array is null.
    JButton[][] slots = new JButton[6][7];
    for(int i = 0; i < slots.length; i++) {
        for(int x = 0; x < slots[i].length; x++) {
            int n = i*slots[i].length + x+1;
            // Instantiate each element of the array.
            // Try this with and without this line.
            slots[i][x] = new JButton(String.valueOf(n));
            if(slots[i][x] == null)
                System.out.println("slots["+i+"]["+x+"] = " +
                                    slots[i][x]);
            else
                System.out.println("slots["+i+"]["+x+"].text = " +
                                    slots[i][x].getText());
        }
    }

Similar Threads

  1. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 02:34 PM
  2. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM
  3. how to change the appearance of jbuttons
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:26 PM
  4. JButtons in a straight line on the BOTTOM
    By paul in forum AWT / Swing
    Replies: 0
    Last Post: 07-16-2007, 04:47 PM
  5. disabling JButtons after win in TicTacToe
    By noisepoet in forum New To Java
    Replies: 1
    Last Post: 05-18-2007, 11:01 PM

Posting Permissions

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