Solved

JRadioButtons in a JTable

Posted on 2006-07-17
8
698 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
Industry Leaders: 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
jsp insert to database example 2 90
Java: anonymous class 4 55
java imports not found 4 71
Java 8 to Java 6 8 43
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

751 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