Solved

JRadioButtons in a JTable

Posted on 2006-07-17
8
682 Views
Last Modified: 2012-06-22
Hi all,

I'm looking at this example on objects website:
http://www.objects.com.au:8088/objects/java/examples/swing/RadioButtonTableCell2.do

But it does not quite work. When you click on a radiobutton the first time, everything is deselected, and then on the second click it selects the JRadioButton. Can somebody please tell me how to sort this, since I'm just not seeing it.

Thanks in advance
0
Comment
Question by:riaancornelius
  • 3
  • 3
  • 2
8 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17120774
I cannot see the link, I am getting a The requested URL could not be retrieved error. Which table are you referring to, the firts or second from here: http://www.esus.com/javaindex/j2se/jdk1.2/javaxswing/editableatomiccontrols/jtable/jtableradiocell.html ?
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17120806
the second
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17120866
those examples are getting so out of date
i've updated a few but that ones stiull in the works.

what you basically need to do is change the editor to the follwoing

  class RadioButtonEditor extends AbstractCellEditor implements TableCellEditor {
    RadioButtonPanel panel;

    public RadioButtonEditor(RadioButtonPanel panel) {
      this.panel = panel;
      ButtonGroup buttonGroup = new ButtonGroup();
      JRadioButton[] buttons  = panel.getButtons();
      for (int i=0; i<buttons.length; i++) {
        buttonGroup.add(buttons[i]);
      }                  
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
                   boolean isSelected, int row, int column) {
      if (value instanceof Integer) {
        panel.setSelectedIndex(((Integer)value).intValue());
      }      
      return panel;
    }

    public Object getCellEditorValue() {
      return new Integer(panel.getSelectedIndex());
    }
  }

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 92

Expert Comment

by:objects
ID: 17120873
btw, correct url is

http://www.objects.com.au/java/examples/swing/RadioButtonTableCell2.do

but that may be changing soon, as we have a new site release in the pipeline.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17120926
riaancornelius,
> the second

Ok I see what you mean. Try objects' suggestion, it should work.
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17121010
Objects, your suggestion works within the current row, but as soon as the row loses focus, both my radiobuttons are selected. Is there maybe a problem in the renderer?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 17121040
try adding the radio buttons to a button group in the panel ctor
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 17121066
lol, I just tried that. Yes, this sorts it:

class RadioButtonPanel extends JPanel {
    JRadioButton[] buttons;
   
    public RadioButtonPanel(String[] str) {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        buttons = new JRadioButton[str.length];
        ButtonGroup buttonGroup = new ButtonGroup();
        for (int i=0; i<buttons.length; i++) {
            buttons[i] = new JRadioButton(str[i]);
            buttonGroup.add(buttons[i]);
            buttons[i].setFocusPainted(false);
            add(buttons[i]);
        }
    }
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now