Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 17th, 2007, 5:48 PM   #1
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
for loop variables

Hello. I have a need to create 81 buttons (named btn1...btn81).

Instead of typing out
JButton btn1 	= new JButton("0")
JButton btn2 	= new JButton("0")
...
JButton btn81 	= new JButton("0")

is there a way I can use a for loop to do this
for(int i=0; i<=81; i++)
{
     JButton btni 	= new JButton("0")
}
obviously this would probably not work for two reasons it would just make a button called btni 81 times probably, also i believe there would be scope issues?


How can I do this?
or do I just have to type them all out?

Thank you
emdiesse is offline   Reply With Quote
Old May 17th, 2007, 6:02 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
Use an array of JButton.

Edit: Just read that wrong. If you _really must_ have them named btn1..btn81 instead of just accessing the correct index of the array then yes, you'll have to type them out.
andro is offline   Reply With Quote
Old May 17th, 2007, 6:17 PM   #3
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
Ah no, thats nasty just a huge block of buttons.
also. I just i can't add them to the frame using a for loop either.

Oh, this means I have to put some effort in.
emdiesse is offline   Reply With Quote
Old May 17th, 2007, 7:13 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 931
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Why not just use an array?
titaniumdecoy is offline   Reply With Quote
Old May 17th, 2007, 8:18 PM   #5
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
I didn't know that could be done with buttons.

Also, misread andros message. I didn't see the part saying if it was a must to have them named btn1 to btn81.

I have this now
JButton[][]btn = new JButton[9][9];
		for(int r=0;r<9; r++)
		{
			for(int c=0;c<9; c++)
			{
				btn[r][c].setSize(25,25);
				btn[r][c].setText("0");

				pane.add(btn[r][c]);
			}
		}

but when I run it I just get a blank frame.
emdiesse is offline   Reply With Quote
Old May 17th, 2007, 9:11 PM   #6
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 172
Rep Power: 4 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
What is Pane? Variable? If so What kind? Are you updating the frame? I'm not used to working with Java frames and Panels but I'm guessing you have to make sure you update the frame somehow.
PhilBon is offline   Reply With Quote
Old May 17th, 2007, 9:32 PM   #7
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
For starters, you need to add:

btn[r][c] = new JButton("0");

Before you go and try to add them.
andro is offline   Reply With Quote
Old May 17th, 2007, 9:38 PM   #8
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
Quote:
Originally Posted by andro View Post
For starters, you need to add:

btn[r][c] = new JButton("0");

Before you go and try to add them.
Excellent!
You just solved my problem
emdiesse is offline   Reply With Quote
Old May 17th, 2007, 11:20 PM   #9
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Is a 2D array necessary for your JButtons? Why not just make a single JButton with a length of 81?
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing Variables with Multiple Dialogs/Forms PhilBon Visual Basic .NET 6 Oct 11th, 2006 8:14 AM
[Efficiency] Variables vs. Calculations kurt C 7 Dec 29th, 2005 3:39 PM
Assignment of numbers to variables without asking Haz C# 26 May 23rd, 2005 11:30 AM
Is it possible for me using session variables into a class? see07 C# 1 Mar 9th, 2005 6:32 PM
global variables uman C++ 2 Feb 20th, 2005 11:39 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC