Hi Everyone,

I am playing around with some Java following some tutorial online and ran into a problem that I cant seem to figure out. In the code below, inside of the groupButton method I get the error 'JRadioButton1,JRadioButton2 and JRadioButton3 cannot be resolved to a variable'. Can you please help me identify the problem?

package form_controls_lesson;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.TextField;

import javax.swing.JTextField;

import java.awt.Color;

import javax.swing.JCheckBox;

import java.awt.TextArea;

import javax.swing.JRadioButton;


@SuppressWarnings("serial")
public class FormObjects extends JFrame {

    private JPanel contentPane;
    private JTextField txtComboBoxItem;
    private final TextArea taOne = new TextArea();


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FormObjects frame = new FormObjects();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    private void groupButton(){
        ButtonGroup bg1 = new ButtonGroup();

        bg1.add(jRadioButton1);
        bg1.add(jRadioButton2);
        bg1.add(jRadioButton3);
    }

    /**
     * Create the frame.
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public FormObjects() {
        groupButton();      
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 498, 466);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JComboBox comboOne = new JComboBox();
        comboOne.setForeground(Color.BLUE);
        comboOne.setBackground(Color.CYAN);
        comboOne.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                String itemText=(String)comboOne.getSelectedItem();
                txtComboBoxItem.setText(itemText);

            }

        });     

        comboOne.setModel(new DefaultComboBoxModel(new String[] {"C Sharp", "Java", "PHP", "Visual Basic .NET"}));
        comboOne.setBounds(12, 13, 120, 22);
        contentPane.add(comboOne);

        JButton btnComboBox = new JButton("Get Drop Down Item");
        btnComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnComboBox.setBounds(144, 13, 149, 22);
        contentPane.add(btnComboBox);

        txtComboBoxItem = new JTextField();
        txtComboBoxItem.setBounds(305, 13, 116, 22);
        contentPane.add(txtComboBoxItem);
        txtComboBoxItem.setColumns(10);

        JPanel panel = new JPanel();
        panel.setBounds(12, 69, 120, 139);
        contentPane.add(panel);
        panel.setLayout(null);

        final JCheckBox jCheckBox1 = new JCheckBox("C Sharp");
        jCheckBox1.setBounds(0, 9, 113, 25);
        panel.add(jCheckBox1);

        final JCheckBox jCheckBox2 = new JCheckBox("Java");
        jCheckBox2.setBounds(0, 41, 113, 25);
        panel.add(jCheckBox2);

        final JCheckBox jCheckBox3 = new JCheckBox("PHP");
        jCheckBox3.setBounds(0, 71, 113, 25);
        panel.add(jCheckBox3);

        final JCheckBox jCheckBox4 = new JCheckBox("Visual Basic");
        jCheckBox4.setBounds(0, 101, 113, 25);
        panel.add(jCheckBox4);

        JButton btnCheckBoxes = new JButton("Selected Items");
        btnCheckBoxes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String s1="";
                if (jCheckBox1.isSelected()){
                    s1=s1+" "+jCheckBox1.getText()+ '\n';
                }
                if (jCheckBox2.isSelected()){
                    s1=s1+" "+jCheckBox2.getText()+ '\n';
                }
                if (jCheckBox3.isSelected()){
                    s1=s1+" "+jCheckBox3.getText()+ '\n';
                }
                if (jCheckBox4.isSelected()){
                    s1=s1+" "+jCheckBox4.getText()+ '\n';
                } 
                taOne.setText(s1);  }

            });
        btnCheckBoxes.setBounds(162, 127, 120, 22);
        contentPane.add(btnCheckBoxes);
        taOne.setBounds(288, 69, 133, 139);
        contentPane.add(taOne);

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(12, 254, 143, 108);
        contentPane.add(panel_1);
        panel_1.setLayout(null);

        JRadioButton jRadioButton2 = new JRadioButton("Debit Card");
        jRadioButton2.setBounds(24, 39, 127, 25);
        panel_1.add(jRadioButton2);

        JRadioButton jRadioButton1 = new JRadioButton("Credit Card");
        jRadioButton1.setBounds(24, 9, 93, 25);
        panel_1.add(jRadioButton1);

        JRadioButton jRadioButton3 = new JRadioButton("PayPal");
        jRadioButton3.setBounds(24, 69, 127, 25);
        panel_1.add(jRadioButton3);



    }




}

Dani AI

Generated

— this is a plain scope problem. The three JRadioButton variables in your constructor are local to that constructor, so groupButton() cannot see them. As noted, those names only exist inside the constructor block, and you call groupButton() before the radio buttons are created.

Three simple fixes (pick one):

  • Promote the buttons to instance fields: add private JRadioButton jRadioButton1, jRadioButton2, jRadioButton3; at class level, then in the constructor assign to those fields (no JRadioButton type there) and call groupButton() after you create them.

  • Pass the buttons into the helper: change groupButton to accept the buttons and call it after instantiation. Example pattern:

    private void groupButton(JRadioButton... buttons) {
        ButtonGroup bg1 = new ButtonGroup();
        for (JRadioButton b : buttons) bg1.add(b);
    }

    Then call groupButton(jRadioButton1, jRadioButton2, jRadioButton3); after creating them.

  • Or simply move the existing groupButton(); call to after the three local JRadioButton creations and change groupButton to accept those locals.

Small style note: prefer private instance fields for UI controls you need across methods, and always initialize them before calling helper methods that use them. That will remove the compile error and make the code clearer.

jRadioButton1, jRadioButton2, jRadioButton3 don't exist in "groupButton". They are only in the constructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.