This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes accessing dynamically generated Jbuttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "accessing dynamically generated Jbuttons" Watch "accessing dynamically generated Jbuttons" New topic
Author

accessing dynamically generated Jbuttons

Carl John
Ranch Hand

Joined: Jun 14, 2010
Posts: 47

hi..!
i'm generating jbuttons dynamically inside a jpanel. i want to write event for those buttons. Please guide me to do this..! Thanks in advance..


CJohn
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
So, how do you create buttons dynamically? Is there by any chance a collection of buttons? No crystal ball here. There's no difference in defining one button or a bunch of buttons, so you'd better provide a more thourough description of your 'problem'.

We got a topic around here on danymic menu items ... maybe you want to look it up (it's not that different, because menu items and buttons are handled very similar):
http://www.coderanch.com/t/498424/GUI/java/applying-actionlistener-dynamic-submenu


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Carl John
Ranch Hand

Joined: Jun 14, 2010
Posts: 47

Peter Taucher wrote:So, how do you create buttons dynamically? Is there by any chance a collection of buttons? No crystal ball here. There's no difference in defining one button or a bunch of buttons, so you'd better provide a more thourough description of your 'problem'.

We got a topic around here on danymic menu items ... maybe you want to look it up (it's not that different, because menu items and buttons are handled very similar):
http://www.coderanch.com/t/498424/GUI/java/applying-actionlistener-dynamic-submenu



I had a JFrame which contains one jbutton and a jpanel. then i'm adding jbuttons like this..

here pan--> jpanel
//
//
int number = Integer.parseInt(JOptionPane.showInputDialog("Enter the number:"));
pan.removeAll();
for(int i=0;i<number;i++){
pan.setLayout(new GridLayout(4,2));
button = new JButton("Button"+i);
button.setName("Button"+i);
pan.add(button);
pan.revalidate();
}

later i want to access those buttons and get their names by click event..>
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Ok, so I'm using exactly the same action as in the referenced thread (in exactly the same way):
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Here is a similiar example to get you started: http://forums.sun.com/thread.jspa?forumID=57&threadID=609795
Carl John
Ranch Hand

Joined: Jun 14, 2010
Posts: 47

Peter Taucher wrote:Ok, so I'm using exactly the same action as in the referenced thread (in exactly the same way):






Thanks peter..!It works great..!A ton of Thanks to u..!








Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Did you understand my suggestion which is slightly more efficient because you only need to create a single ActionListener, not a unique ActionListener for every button?
Carl John
Ranch Hand

Joined: Jun 14, 2010
Posts: 47

Rob Camick wrote:Did you understand my suggestion which is slightly more efficient because you only need to create a single ActionListener, not a unique ActionListener for every button?



Hi Rob..!

Thanks for your suggestion.It Works fine...i got the solution for that....!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
. . . and welcome to the Ranch, Carl John.
 
Consider Paul's rocket mass heater.
 
subject: accessing dynamically generated Jbuttons
 
Similar Threads
creating buttons with graphics
MouseMotionListener and JButtons
JButton slow response time
accessing dynamically generated Jbuttons
How to add JLabel In a JTable cell