Why does my ButtonGroup give me an “<identifier> expected” error?

I'm teaching myself about Java's swing components by creating a dialog box with three radio buttons, a text box and a "go" button. I've gotten to the point where I'm adding the Radio Buttons, and am running into an error with my code. I've got one class that calls a constructor to display the dialog box. The text field and "Go" button have no functionality yet; they're just placeholders.

When I run the code as posted below, a dialog box displays as expected, but I am able to select all three radio buttons simultaneously. Reading Oracle's JButton and "How to use Radio Buttons" documentation and tutorials, it appears that I need to apply a ButtonGroup to my JRadioButtons, which automatically groups them with a select-one-and-only-one relationship.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//
public class CreateButtonSel {
    public static void main(String[] args) {
        ButtonSel thisButtonSel = new ButtonSel();
        final int WIDTH = 250;
        final int HEIGHT = 250;
        thisButtonSel.setSize(WIDTH,HEIGHT);
        thisButtonSel.setVisible(true);
    }
}

However, when I un-comment the noted code in my ButtonSel class, despite appearing to follow the syntax noted in the tutorial for a ButtonGroup returns error: <identifier> expected with carats under the open and close parens of my ...Button.add(...) statements. This error, from searching stack, seems to be associated with declaring varibale improperly, or at the wrong place in the class. I can't spot that error. import javax.swing.* should give me access to all the Button constructors, right? Is there another I should be pulling in?

What am I missing here?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//
public class ButtonSel extends JFrame {
    JLabel buttonSelLabel = new JLabel("Select One");
    JRadioButton oneButton = new JRadioButton("One", true);
    JRadioButton twoButton = new JRadioButton("Two", false);
    JRadioButton threeButton = new JRadioButton("Three", false);
/*    comment out the following; it runs fine.
 *    uncomment, and I get the following error: 
 *
 *    error: <identifier> expected with carats under the open and close 
 *    parens of my ...Button.add(...) statements. 
 *
    ButtonGroup groupOfRadioButtons = new ButtonGroup();
    groupOfRadioButtons.add(oneButton);
    groupOfRadioButtons.add(twoButton);
    groupOfRadioButtons.add(threeButton);   
*/
    JButton approveButton = new JButton("Go");
    JTextField textDisplay = new JTextField(18);
     //
    JPanel timerPanel = new JPanel();
    JPanel radioPanel = new JPanel(new GridLayout(0, 1));
     //
    public ButtonSel() {
        super("ButtonTest");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        add(buttonSelLabel);
        add(oneButton);
        add(twoButton);
        add(threeButton);
        add(textDisplay);
        add(approveButton);
    }
}

Answers


You're trying to execute statements here, but without them being in a method or constructor:

ButtonGroup groupOfRadioButtons = new ButtonGroup();
groupOfRadioButtons.add(oneButton);
groupOfRadioButtons.add(twoButton);
groupOfRadioButtons.add(threeButton); 

A class only directly contains method, constructor and nested type declarations, and initializer blocks.

You should put the three statements after the declaration into the constructor - you could use an initializer block, but it's not generally good in terms of readability.

public class ButtonSel extends JFrame {
    ... other fields ...

    // Note: it's a good idea to make all fields private
    private ButtonGroup groupOfRadioButtons = new ButtonGroup();

    public ButtonSel() {
        super("ButtonTest");
        ...
        groupOfRadioButtons.add(oneButton);
        groupOfRadioButtons.add(twoButton);
        groupOfRadioButtons.add(threeButton);
    }
}

Need Your Help

Getting: “java.lang.NullPointerException” but I know object is instantiated in AsyncTask

java android android-asynctask nullpointerexception

I am attempting to pass a string I got in an Asynchronous task class back in to my main activity, but when I pass the string result (which I know isn't null because logging the string right before

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.