Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Event handling for dynamically created components

 
Henry ostley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 52664
122
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Randall Twede
Ranch Hand
Posts: 4521
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • 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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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: 52664
122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are creating that sort of class, consider an AbstractAction, which Rob Spoor introduced me to once.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!