Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8440899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:25:33+00:00 2026-06-10T08:25:33+00:00

I used this guide in order to make JTable that would handle radio buttons.

  • 0

I used this guide in order to make JTable that would handle radio buttons. Works fine except i need to enable a default enabled button.

there can be n rows. I’ve tried to enable it through the default table model, the Object[][], the table, and i tried enabling the button before adding it to the Object[][]. I couldn’t figure out how(if it is possible) to do it with the buttongroup.

to find the default enabled button i have to compare the button text to a string(this part works).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:25:34+00:00Added an answer on June 10, 2026 at 8:25 am

    Not sure that I am interpreting the question correctly. You can use JRadioButton constructor to set selection, for example the snippet (based on OP code sample) will set selected button “B”:

    dm.setDataVector(new Object[][] { { "Group 1", new JRadioButton("A") },
        { "Group 1", new JRadioButton("B", true) },
        { "Group 1", new JRadioButton("C") },
        { "Group 2", new JRadioButton("a") },
        { "Group 2", new JRadioButton("b") } }, new Object[] {
        "String", "JRadioButton" });
    

    You can also change selection like this:

    ((JRadioButton) dm.getValueAt(0, 1)).setSelected(true);
    

    You can also use ButtonGroup.setSelected() method.

    EDIT: eliminate components from model

    The model should contain data rather than components. Storing components in the model defeats the idea of renderers and editors. For more details see Editors and Renderers and Swing Models and Renderers. Check out the following example the mimics button group behavior in the model:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.plaf.*;
    import javax.swing.table.*;
    
    public class ButtonGroupMockupTest {
        private static void createAndShowGUI() {
            DefaultTableModel model = new DefaultTableModel(new Object[][] {
                    { "Group 1", Boolean.FALSE }, { "Group 2", Boolean.FALSE },
                    { "Group 3", Boolean.FALSE } },
                    new Object[] { "Name", "State" }) {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public Class getColumnClass(int col) {
                    if (col == 1)
                        return Boolean.class;
                    return super.getColumnClass(col);
                }
    
                @Override
                public void setValueAt(Object value, int row, int col) {
                    super.setValueAt(value, row, col);
                    if (col == 1 && value.equals(Boolean.TRUE))
                        deselectValues(row, col);
                }
    
                private void deselectValues(int selectedRow, int col) {
                    for (int row = 0; row < getRowCount(); row++) {
                        if (getValueAt(row, col).equals(Boolean.TRUE)
                                && row != selectedRow) {
                            setValueAt(Boolean.FALSE, row, col);
                            fireTableCellUpdated(row, col);
                        }
                    }
                }
            };
    
            JTable table = new JTable(model);
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.setDefaultRenderer(Boolean.class, new BooleanRadionRenderer());
            table.setDefaultEditor(Boolean.class, new BooleanRadioEditor());
    
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new JScrollPane(table));
    
            f.pack();
            f.setLocationByPlatform(true);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        static class BooleanRadionRenderer implements TableCellRenderer, UIResource {
            JRadioButton radioButton;
            Border emptyBorder;
    
            public BooleanRadionRenderer() {
                radioButton = new JRadioButton();
                radioButton.setHorizontalAlignment(JRadioButton.CENTER);
                radioButton.setBorderPainted(true);
                emptyBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
            }
    
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int col) {
                if (isSelected) {
                    radioButton.setBackground(table.getSelectionBackground());
                    radioButton.setForeground(table.getSelectionForeground());
                } else {
                    radioButton.setBackground(table.getBackground());
                    radioButton.setForeground(table.getForeground());
                }
                if (hasFocus)
                    radioButton.setBorder(UIManager
                            .getBorder("Table.focusCellHighlightBorder"));
                else
                    radioButton.setBorder(emptyBorder);
    
                radioButton.setSelected(((Boolean) value).booleanValue());
                return radioButton;
            }
        }
    
        static class BooleanRadioEditor extends AbstractCellEditor 
                                        implements TableCellEditor {
            private static final long serialVersionUID = 1L;
            private JRadioButton radioButton;
    
            public BooleanRadioEditor() {
                radioButton = new JRadioButton();
                radioButton.setHorizontalAlignment(JRadioButton.CENTER);
                radioButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        // prevent deselection to mimic button group
                        if (!radioButton.isSelected())
                            cancelCellEditing();
                        stopCellEditing();
                    }
                });
            }
    
            @Override
            public Component getTableCellEditorComponent(JTable table, Object value,
                    boolean isSelected, int row, int col) {
                radioButton.setSelected(((Boolean) value).booleanValue());
                return radioButton;
            }
    
            @Override
            public Object getCellEditorValue() {
                return Boolean.valueOf(radioButton.isSelected());
            }
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i used this code: List<string> lists=new List<string>(apple,orange,banana,apple,mang0,orange); string names; names=lists.Distinct() is that correct?
I used this MSDN example to construct my console host app: http://msdn.microsoft.com/en-us/library/ms731758.aspx It works
I used this guide: http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder to be able to create my own custom UITableViewCell
I'm using this guide to create a HTTPHandler that combines script and css files
I used this tutorial to create a basic Javascript function for implementing change during
i used this script to animate some divs with jquery and css. now i
I used this link to create a SHA1 hash for any data using C++.
I used this selector in a function and I don't even know exactly what
I used this tutorial to rename a view controller http://iosdeveloperzone.com/2011/05/14/how-to-rename-a-class-using-refactoring-in-xcode-4/ Basically just switch to
I used this code to draw a line in a panel. private bool isMoving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.