Solved

JRadioButtons in a JTable

Posted on 2006-07-17
8
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 56
spring jars download 1 52
Java class and jar 3 42
Java List 4 35
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

756 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