Solved

JRadioButtons in a JTable

Posted on 2006-07-17
8
664 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
Comment Utility
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
Comment Utility
the second
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Expert Comment

by:girionis
Comment Utility
riaancornelius,
> the second

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

Author Comment

by:riaancornelius
Comment Utility
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
Comment Utility
try adding the radio buttons to a button group in the panel ctor
0
 
LVL 9

Author Comment

by:riaancornelius
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 48
strCount chalenge 3 50
Which is the latest version of Apache Tomcatl 3 35
Java asynchronous logging 4 31
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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