Wednesday, November 2, 2011
How to Auto Generate Buttons in JAVA
Well this is something amazing You can create any number of Buttons using an Array
This kind of code you will need to use probably if you don't know how many buttons you need in your program. You can create many buttons as you want using lesser lines.
Here goes the codes........... Enjoy
starter_itemCounter =100; // This stores the element number in button array
JButton[] buttonArray = new JButton[starter_itemCounter]; // Create the array
int Bcounter=1; // Used to count button names. we store names for each button in dofferent array
/* Count the 'y position' place of the JButton and JLabel in GUI. This helps the button to fall one after another */
int place=25;
int starterCounter = 1; // Count button array items
while(Bcounter < starter_itemCounter && starterCounter < starter_itemCounter)
{
// Print JButton
/* Create a new button and add button name from button name array */
buttonArray[starterCounter] = new JButton(""+starter_name[Bcounter]);
buttonArray[starterCounter].setBounds(25, place, 300, 25);
panel.add(buttonArray[starterCounter]);
starterCounter++;
Bcounter++;
place += 46;
I used this code to create a hotel menu system which auto generate buttons according to the food names stored in the hotel Data Base. Customer only has to click a button to place an order
An image of the Hotel Menu System's GUI is shown at the top of this post
thank you :)
Subscribe to:
Post Comments (Atom)
no codes only symbols.
ReplyDelete