• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating controls at run time

 
krishna balaji
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic