954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with a button array...

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

Shaun1987
Newbie Poster
6 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

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:

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

See the pattern yet?

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

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

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	
}
fdrage
Light Poster
28 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

Shaun1987
Newbie Poster
6 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

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]);
}

sowmyask7
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

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

Zibo
Light Poster
41 posts since May 2009
Reputation Points: 12
Solved Threads: 3
 

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

sowmyask7
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

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


thanx

anand008
Newbie Poster
1 post since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You