Hi, I am very new to Java and am using Breezy Swing. I basically need to create an array of JButtons (26 in total) and display them on a form (9 on the top row, 9 in the middle and 8 on the bottom) and I have no idea of how to do this... As I said I am very new to this.

I can create a standard 1D array containing strings etc... so I assume that it uses the same syntax. Can anyone help me with this?

Thanks in advance!

Shaun

Member Avatar for iamthwee

Hi, I am very new to Java and am using Breezy Swing. I basically need to create an array of JButtons (26 in total) and display them on a form (9 on the top row, 9 in the middle and 8 on the bottom) and I have no idea of how to do this... As I said I am very new to this.

I can create a standard 1D array containing strings etc... so I assume that it uses the same syntax. Can anyone help me with this?

Thanks in advance!

Shaun

Read a good book or tutorial. Then when you have some actual code going, post it here with relevant questions. Tee he he.

:cool:

Object[] = 1D
Object[][] = 2D
Object[][][] = 3D

See the pattern yet?

As for laying them out, I'd probably go with a GridBag layout.

GridBagLayout is prolly the way...
as for creating the JButtons... its a little something like this

String[] numbers = {"0", "1", "2", "3", 4", "5", "6", "7", "8", "9"}; // goes on to 26
JButtons[] buttons = new JButton(numbers.length)
// create instance of each button
for (int i = 0; i < numbers.length; i++){
buttons[i] = new JButton(numbers[i]); // create buttons	
}

Fantastic! Worked a treat! Thanks :) Now I just have the matter of the on click event. Now for standard buttons that are not part of the array it's easy and I assumed that it would be similar for array'd buttons like this:

   if (buttonObj == buttons[0]) {
       generateRandomWord();
   }

But I was wrong!! Could anyone shed any light on this for me? Thanks again!!

Shaun

First you have to declare the array of control's that you want

 private JTextField[] txt =  new JTextField[10];
 private  JButton[] button = new JButton[10];

then initilase an instance of that perticular control,like below

 JTextField t = new JTextField();
 JButton btn1 = new JButton("button");

then call this method , that you want to generate onclick event

public void add(){
        ++iCount; // to keep count
         JButton btn = new JButton("button");
         JTextField t = new JTextField();
        txt[iCount]=t;
        button[iCount]=btn;
        jPanel2.setLayout(new GridLayout(0,6,5,5)); //how many columns you want in the grid    
        jPanel2.add(txt[iCount]);        
        jPanel2.add(button[iCount]);
       }

I believe, that during those 3 years he managed to add those buttons... But I see You are making sure that he did.

how to set the events dynamically for the array of buttons .

sir can i get codes for using with frame
i m trying but not getting correctly plz hel me out


thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.