This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JRadioButtonList error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JRadioButtonList error" Watch "JRadioButtonList error" New topic
Author

JRadioButtonList error

Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Hi all,
I am trying to implement a JList with JRadioButton. My expected result is; only one element of this list should be selected at a time. Below is a code i have attempted for this.
Problem is, I am unable to group these buttons for single selection. Can anyone please help me?



Regards,
Mahesh Kedari - Fidus Technologies Ltd.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
I would suggest you try a single column JTable instead of a JList. A JTable is designed to support editing, via a mouse click. This page has an old example that might get you started.
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Thanks alot Rob for your reply.
Well, editing the record is not my requirement. I wanted to bind this list to data base and selected record in this list to one of the UI component.
But for this purpose, i need user to select only one record. I have put ListSelectionModel.SINGLE_SELECTION, but still not sure why all the radio buttons are getting selected.
I also have put a ButtonGroup in this example, still multiple radio buttons are getting selected at the same time.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
Well, editing the record is not my requirement


The "editing" of a JTable allows you to click on a JCheckbox to select/deselect it. This same basic functionality should work for a JRadoButton as well as the example demonstrates.
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Rob, this is possible with JList also. The example is given in Swing Hacks book.
Please see below example which works fine for check box. If i replace check box with radio button, it still works fine.
But the problem is, when you group these radio buttons for only one element to remain selected at a time, this code fails.
I need your help to track where exactly I am going wrong in my code.



Thanks for your help.>
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
this is possible with JList also.


I didn't say it wasn't possible.

The example is given in Swing Hacks book.


Exactly, its a "hack". This functionality is built in as part of JTable. JTable doesn't do exactly what you want out of the box which is why I gave you the link. It may be simpler to implement than your current hack. I prefer to build on existing functionality rather than reinvent the wheel completely. Only you can decide which is the better approach.
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Yup Rob...
You were absolutely right... I got the solution... Thanks aloooooooooooooot....!!!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Mahesh Kedari wrote:Hi all,
I am trying to implement a JList with JRadioButton. My expected result is; only one element of this list should be selected at a time.

JList already has a selection mechanism and you have even referred to it in your follow-up post. So why the radio button?


luck, db
There are no new questions, but there may be new answers.
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Well Darryl the purpose of radio button is to show the selection although nothing is highlighted in JList.
Actually the main purpose of this requirement is about UI design only. Its going to work in the similar way as the SINGLE_SELECTION_MODE of JList selection model.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Note that for efficiency, you should override several methods of the JRadioButton to do nothing. Read the API for DefaultListCellRenderer to know which methods. And if you want a selection background to be used, modify the code accordingly.
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Thanks alot Darryl,
This is what I was looking for...!!!
I think i was unnecessarily making it more complected.
albert thomsan
Greenhorn

Joined: Oct 18, 2010
Posts: 5
You first initialze your whole list's selection value false. That is list(i).enable = false . I don't write the whole syntax but i just give you the overall idea. When you do like this all list of elements are not selected. When user select one value you just enable that element state to true. In this way you can select one element at a time.


Everything is possibe in the world.You just keep up the hard work and you will get the success.
 
Don't get me started about those stupid light bulbs.
 
subject: JRadioButtonList error
 
Similar Threads
JList with variable row height
How to add icons along with the text in a JList[urgent]
JToolTip displays JList selected text, later
how to highlight impt date in JList..?
Changing the values in the list box