Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JRadioButtons in a JTable

Posted on 2006-07-17
8
Medium Priority
?
711 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 2000 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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