File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating controls at run time" Watch "creating controls at run time" New topic

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

marc weber

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
I agree. Here's the link:
subject: creating controls at run time
It's not a secret anymore!