aspose file tools*
The moose likes Beginning Java and the fly likes creating controls at run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating controls at run time" Watch "creating controls at run time" New topic
Author

creating controls at run time

krishna balaji
Ranch Hand

Joined: Mar 16, 2006
Posts: 31
i want to create a componets in swing at run time as control array.

Button b[]=new button[10];

will this work?

how can i change the number of elements in array at run time.

if possible can u demonstarte with an example code.please


THANK U FRIENDS
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

Certainly you can use an array to hold references to things like buttons. To set the number of elements at runtime, just pass that number as a variable...

JButton[] myButtons = new JButton[numberOfButtons];

Note, however, that once an array is instantiated, it cannot be resized. For that flexibility, you need something like an ArrayList.

Here's an example in which the command line argument specifies the number of buttons at runtime...

[ March 16, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: creating controls at run time