• 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

JRadioButtonList error

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup Rob...
You were absolutely right... I got the solution... Thanks aloooooooooooooot....!!!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Darryl,
This is what I was looking for...!!!
I think i was unnecessarily making it more complected.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic