Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing dynamically generated Jbuttons

 
Carl John
Ranch Hand
Posts: 47
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Carl John
Ranch Hand
Posts: 47
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I'm using exactly the same action as in the referenced thread (in exactly the same way):
 
Rob Camick
Ranch Hand
Posts: 2583
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a similiar example to get you started: http://forums.sun.com/thread.jspa?forumID=57&threadID=609795
 
Carl John
Ranch Hand
Posts: 47
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2583
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch, Carl John.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic