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 741211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:36:22+00:00 2026-05-14T08:36:22+00:00

I’m stumped… I’m trying to use JGoodies binding to bind a radiobutton to an

  • 0

I’m stumped… I’m trying to use JGoodies binding to bind a radiobutton to an Enum property. I can’t seem to get it to work. Below is a simple example, it’s a table of beans each with an immutable number and a mutable “adjective” property. The radiobuttons are bound to the “adjective” property. Clicking on the radio buttons does change the adjective, and selecting a new item in the table does update the radio buttons, but pressing a radio button does not clear the other radio buttons. Why is this, and how do I fix it?

And do I need to use PropertyChangeSupport? (it doesn’t seem to make any difference)

package com.example.jgoodies;

import java.beans.PropertyChangeSupport;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import com.jgoodies.binding.adapter.Bindings;
import com.jgoodies.binding.beans.BeanAdapter;
import com.jgoodies.binding.value.ValueModel;

public class RadioButtonBindingExample extends JFrame {

    static public <T> T pick(T[] values, int i)
    {
        int j = i % values.length;
        if (j < 0)
            j += values.length;
        return values[j];
    }
    enum Adjective { SHORT, TALL, RED, GREEN;
        @Override public String toString() { return name().toLowerCase(); }
    }

    static public class NumberBean
    {
        final private PropertyChangeSupport pcs = new PropertyChangeSupport(this); 
        final private int n;
        private Adjective adjective;

        public NumberBean(int n) { 
            this.n = n;

            this.adjective = pick(Adjective.values(), n);
        }
        public int getNumber() { return this.n; }
        public Adjective getAdjective() { return this.adjective; }
        public void setAdjective(Adjective a) { 
            Adjective old = this.adjective;
            this.adjective = a; 
            this.pcs.firePropertyChange("adjective", old, a);
        }
        public String getLabel() { return this.adjective + " " + n; }
    }

    final BeanAdapter<NumberBean> pNumber = 
        new BeanAdapter<NumberBean>((NumberBean)null); 

    final private NumberBeanTableModel numberBeanDataModel = new NumberBeanTableModel(100);
    final private JPanel numberBeanDisplayPanel = new JPanel();

    public static class NumberBeanTableModel extends AbstractTableModel {
        final private NumberBean[] v;

        public NumberBeanTableModel(int nrows)
        {
            this.v = new NumberBean[nrows];
            for (int i = 0; i < nrows; ++i)
            {
                this.v[i] = new NumberBean(i);
            }
        }

        public int getColumnCount() { return 2; }
        public int getRowCount() { return this.v.length;}
        public NumberBean getNumberBean(int row)
        {
            return this.v[row];
        }
        public Object getValueAt(int row, int col) { 
            NumberBean nb = this.v[row];
            return (col==0 ? nb.getNumber() : nb.getAdjective());
        }
    };

    NumberBeanTableModel getNumberBeanModel() { return this.numberBeanDataModel; }

    public RadioButtonBindingExample(String title)
    {
        super(title);

        JPanel panel = new JPanel();        
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

        JTable table = new JTable(getNumberBeanModel());
        table.getColumnModel().getColumn(0).setHeaderValue("row number");
        final ListSelectionModel lsm = table.getSelectionModel();
        lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsm.addListSelectionListener(new ListSelectionListener() {
            @Override public void valueChanged(ListSelectionEvent event) {
                int i = lsm.getLeadSelectionIndex();
                NumberBean nb = getNumberBeanModel().getNumberBean(i);
                onTableSelection(nb);
            }});
        JScrollPane scrollpane = new JScrollPane(table);

        JLabel rowLabel = new JLabel();
        Bindings.bind(rowLabel, 
                        this.pNumber.getValueModel("label"));

        JPanel subpanel = this.numberBeanDisplayPanel;
        subpanel.setLayout(new BoxLayout(subpanel, BoxLayout.PAGE_AXIS));
        subpanel.add(rowLabel);

        ValueModel rmodel = this.pNumber.getValueModel("adjective");
        for (Adjective adjective : Adjective.values())
        {
            JRadioButton r = new JRadioButton(adjective.toString());
            Bindings.bind(r, rmodel, adjective);
            subpanel.add(r);
        }

        subpanel.setVisible(false);
        panel.add(subpanel);
        panel.add(scrollpane);      
        getContentPane().add(panel);        
    }

    protected void onTableSelection(NumberBean nb) {
        this.pNumber.setBean(nb);
        this.numberBeanDisplayPanel.setVisible(true);
    }

    public static void main(String[] args)
    {
        RadioButtonBindingExample rbbe = 
            new RadioButtonBindingExample("radio button binding example");
        rbbe.pack();
        rbbe.setVisible(true);
        rbbe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
  • 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-05-14T08:36:22+00:00Added an answer on May 14, 2026 at 8:36 am

    DOH! I figured it out. I was missing two things:

    1. Property change support is necessary, I forgot to add these lines to the bean with property change support:

      public void addPropertyChangeListener(PropertyChangeListener pcl)
      {
          this.pcs.addPropertyChangeListener(pcl);
      }
      public void removePropertyChangeListener(PropertyChangeListener pcl)
      {
          this.pcs.removePropertyChangeListener(pcl);
      }
      
    2. The BeanAdapter constructor takes a second argument to ensure it is getting change events from the bean in question:

      final BeanAdapter<NumberBean> pNumber = 
        new BeanAdapter<NumberBean>((NumberBean)null, true); 
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.