-
Array of Button
Help,
I am a new Java programmer and I need some help.
I need to create a 4x4 array of Buttonsinside a panel. I would like to be able to address each button by the index of the array. For instance buttonArray[2][3] would be the 3rd row 3rd column.
Can any one suggest how I would code this?
Thanks
Bill
-
Hi Bill,
You can create array of buttons like the following :
1. Button but=new Button[your_array_size ]
you can also leave array size blank for starting.
2. Add suitable (& of required size) layout manager to your panel.
3. Add the buttons to layout manager.
4. For acessing - but[0,0] is the first button and so on.
Tip: You can identify buttons based on their labels or object names for event handling . Try to assign descritive names to buttons such as "start", "exit" , "move" etc instead of passive array to enhance your coding.
Hope this helps.
Pankaj
-
Oops!
Between step 1 & 2 you also need one more step.
You have initialized the array of Buttons but not yet created its individual objects i.e Buttons so you will have to create each one of these Button namely
but[0,0]=new Button("Button 1");
but[0,1]=new Button("Button 2");
but[0,2]=new Button("Button 3");
and so on.
You have to do this step before you add these buttons to panel.
bye
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks