• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Event handling for dynamically created components

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have created dynamic swing components by using:

These display fine but I am struggling to find out how to dynamically handle the event.
Any help would be hugely appreciated.
Thanks
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you calling that dynamic? I would have thought “label” is a less-than-brilliant name for a button, however.
You need to add Listeners. You can add Listeners to just about everything in Swing. If you find the 3rd edition of Thinking in Java™ by Bruce Eckel, you can download it free of charge, and there is (I think) a little program in it which uses reflection to give you a list of how many different listeners you can add to a particular Component.
I have found some of my old posts about listeners: 1 2 3 Some of them will have links to old posts, and if you follow them far enough, you can get back to where you started!
Have a look at them, and see whether they help. It is easier to post links to old posts than to write them out again. If those links don’t help, tell us more details.
 
Henry ostley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that, I shall have a read through the linked material and get back if I have any further issues.
I copied the wrong bit of code so changed it to jButton from jLabel but didn't change the name my naming isn't too bad in my actual code.
 
Henry ostley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it may be wise if I expand on my problem a little more.
Basically the user selects a number from a combo box which in turn dynamically adds the same number of buttons. I need to be able to handle the events from these buttons.
I can vaguely see a way to do this using anonymous inner classes but am still not entirely sure.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you don't have a problem with having one listener for all the buttons you could put them in an array and then loop through it adding the listener to each
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also create your own button
class MyButton extends JButton

then you can have your listener already
added in MyButton
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are creating that sort of class, consider an AbstractAction, which Rob Spoor introduced me to once.
 
reply
    Bookmark Topic Watch Topic
  • New Topic