• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

accessing dynamically generated Jbuttons

 
Ranch Hand
Posts: 47
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
https://coderanch.com/t/498424/GUI/java/applying-actionlistener-dynamic-submenu
 
Carl John
Ranch Hand
Posts: 47
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so I'm using exactly the same action as in the referenced thread (in exactly the same way):
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....!
 
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch, Carl John.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic