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

Dynamic RadioButton Listeners

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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 ]
 
C. Alan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Pay attention! Tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    Bookmark Topic Watch Topic
  • New Topic