How to listen for events on contained components?

I'm a relative newbie to Java and to Swing components. I've managed to figure out quite a lot in a few weeks from reading and looking at forums, but there's one thing I want to accomplish that I just can't seem to find or understand.

I want to be able to create whole banks of groups of 5-7 radio buttons. These will fit into categories, say three groups of buttons in one category, five groups in another, etc. I have written a class to create a group of buttons on a JPanel, then assign the buttons to a ButtonGroup, and I have implemented listeners in the class, one for each button, which sets a variable to a numeric value depending on which radio button is selected.

With formatting and variable declarations omitted to save space, here it is:

/* ButtonPanel.java

*/

import java.awt.*;

import javax.swing.*;

import java.awt.Event.*;

public class ButtonPanel extends javax.swing.JPanel {

public ButtonPanel(String header) {

this.setLayout(new java.awt.GridBagLayout());

this.add(jLabel1, gridBagConstraints);

jRadioButton1.setText("1");

this.add(jRadioButton1, gridBagConstraints);

jRadioButton2.setText("2");

this.add(jRadioButton2, gridBagConstraints);

jRadioButton3.setText("3");

this.add(jRadioButton3, gridBagConstraints);

jRadioButton4.setText("4");

this.add(jRadioButton4, gridBagConstraints);

jRadioButton5.setText("5");

this.add(jRadioButton5, gridBagConstraints);

jRadioButton6.setText("6");

this.add(jRadioButton6, gridBagConstraints);

jRadioButton7.setText("7");

this.add(jRadioButton7, gridBagConstraints);

ButtonGroup btg = new ButtonGroup();

btg.add(jRadioButton1);

btg.add(jRadioButton2);

btg.add(jRadioButton3);

btg.add(jRadioButton4);

btg.add(jRadioButton6);

btg.add(jRadioButton5);

btg.add(jRadioButton7);

jRadioButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButton1ActionPerformed(evt);

}

});

jRadioButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButton2ActionPerformed(evt);

}

});

jRadioButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButton3ActionPerformed(evt);

}

});

jRadioButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButton4ActionPerformed(evt);

}

});

jRadioButton5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButton5ActionPerformed(evt);

}

});

jRadioButton6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButton6ActionPerformed(evt);

}

});

jRadioButton7.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButton7ActionPerformed(evt);

}

});

// pack();

}// </editor-fold>

private void jRadioButton7ActionPerformed(java.awt.event.ActionEvent evt) {

outval = 7;

}

private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {

outval = 6;

}

private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {

outval = 2;

}

private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {

outval = 3;

}

private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {

outval = 5;

}

private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {

outval = 4;

}

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {

outval = 1;

}

public int pickedButton(){

return outval;

}

}

This simple class seems to work well. My next idea is that I create a new class to implement a category of buttons. The name of the category of buttons can vary, and the labels next to each row (group) of buttons can vary. I implement this by passing an array of labels into the class, using the first as a category header, and the remaining ones as labels for the rows of buttons. The entire category is placed on a JPanel.

/*

* CategoryOfButtons.java

*/

import java.awt.*;

import java.awt.Event.*;

import javax.swing.*;

public class CategoryOfButtons extends javax.swing.JPanel {

public CategoryOfButtons(String[] labels) {

this.setLayout(new java.awt.GridLayout(labels.length+1,1));

this.setBorder(javax.swing.BorderFactory.createEtchedBorder());

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

jLabel1.setFont(new java.awt.Font("Arial Black", 0, 14));

jLabel1.setText(labels[0]);

testBut = new ButtonPanel[labels.length - 1]; //array of button panels

this.add(jLabel1); // the category header

for (i=1;i<=labels.length-1;i++){

testBut[i-1] = new ButtonPanel(labels);

this.add(testBut[i-1]);

};

this.add(jLabel2);

}

// variable declarations omitted to save space

}

From a main class, I will create categories of buttons as needed. Here's what I can't figure. I would like to put a label on the CategoryOfButtons JPanel that keeps a running sum of the selected button from each ButtonPanel, and I want to display that running sum in jLabel2. The obvious solution would seem to be to sum up the testBut[].pickedButton() values over the indices of the testBut array. That works to show an initial value of 0, but I can't figure how to get the value to update. I don't want to have to install a separate button on CategoryOfButtons to cause a fresh evaluation of the sum of the values from the radio buttons.

I tried setting up a mouse clicked action event on CategoryOfButtons, and having the event handler sum up the testBut[].pickedButton() values, but clicking the mouse on a radio button in a contained ButtonPanel doesn't seem to create an event in the CategoryOfButtons container.How can I get a value in the container (CategoryOfButtons) to respond to an event in the contained component (ButtonPanel)?

I hope this makes sense....

[6766 byte] By [dsa763a] at [2007-11-27 10:59:03]
# 1

Your code's pretty hard to read without formatting. If you want to recalculate the sum every time a radio button selection is changed, then do the calculation in the radio button's actionPerformed method.

hunter9000a at 2007-7-29 12:20:26 > top of Java-index,Java Essentials,New To Java...
# 2

In a nutshell, I can't figure out how to listen for the RadioButton event in a container that's two levels up (and implemented by a different class) from where the RadioButton resides.

dsa763a at 2007-7-29 12:20:26 > top of Java-index,Java Essentials,New To Java...
# 3

theRadioButton.addActionListener(theListener);

Hippolytea at 2007-7-29 12:20:26 > top of Java-index,Java Essentials,New To Java...