File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Dynamic RadioButton Listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Dynamic RadioButton Listeners

C. Alan
Ranch Hand

Joined: Dec 17, 2004
Posts: 30
I have an app that admittedly is homework, but it is not a requirement of the assignment. I have a radioButton group of accounts...simple enough, and we have five accounts. Since I like making things harder than they need to be, I decided to create the group dynamically (after all, at some point I might have more than 5 accounts). I also decided it should really be in a scroll panel also, otherwise there will be no room for expansion.

The code to create the button group is pretty trivial:

Basic listener code

I am stuck on the listener part....if I do a getText(), it returns the name of the last button added. I can do a buttonGroup.getSelection() which returns a unique value for each button, but I need to be able to either retrieve the position of the button within the group, or then text of the button to really do anything useful with it.

Anybody have an idea how I can accomplish this? There has to be a way to do it, I just can't see it.
[ March 20, 2005: Message edited by: C. Alan ]

<a href="http://www.security-forums.com/forum/viewforum.php?f=48" target="_blank" rel="nofollow">Malware Removal</a> - Get your system running like new again.
C. Alan
Ranch Hand

Joined: Dec 17, 2004
Posts: 30
I apologize for the duplicate post, but I figured out the solution to my own problem, which usuallly happens five minutes after I ask the question (even if I spet 6 hours trying to figure it out)

Anyway, I made some modifications:

Then in the listener:

Now I am able to get the index of the button pushed, and I can use the index to correlate to the proper account...woot!

It's not pretty, but it works.
javaquests js
Greenhorn

Joined: Mar 05, 2010
Posts: 1
hello sir, This is not an answer to your question but I have similar kind of a problem to use listener for dynamic jtextpane. if you could have an answer to it, please share it with me. i create jtextpanes in loop based on a condition and add the listener inside the loop. but the problem i have is the actionperformed gets called as many times the length of the jtextpanes created. if i can try a similar one like jradiobutton, how can i identify which jtextpane is focussed to call the listener.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the person you are directing your question to hasn't posted here in 5 years.

you are better off starting your own thread, by posting in the Swing forum,
and better still would be to post a compilable/runnable demo program that
displays the problem i.e. don't include anything in the program that is not related to the problem
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Dynamic RadioButton Listeners
 
Similar Threads
How to make label visible
Arrays Won't Work
JFrame panels NOT showing. WHY??
JFrame panels NOT showing. WHY??
Radio Buttons with JScroll Pane