Hi,
Well, I'm using JRadioButton for the first time. So far, I haven't had much luck. I'm trying to check whether one of the radio buttons I have in a
ButtonGroup are selected. I have one of the two radiobuttons selected from the get-go.
The following class is an inner-class. There error is caused when I call
before.isSelected() in the
getSelection() function.
private class radioButtonClass extends JPanel
{
private JRadioButton after;
private JRadioButton before;
private ButtonGroup group;
public radioButtonClass()
{
JRadioButton before = new JRadioButton("Before the current row?", true);
//before.setSelected( true );
JRadioButton after = new JRadioButton("After the current row? ");
group = new ButtonGroup();
group.add( before );
group.add( after );
this.add( before );
this.add( after );
//this.add( group );
//setBackground( new Color(234, 2, 43) );
}
public Dimension getPreferredSize()
{
return new Dimension( 150, 80 );
}
public Dimension getMaximumSize()
{
return getPreferredSize();
}
public int getSelection()
{
//System.out.println("getSelection()");
//System.out.println("Button Count: " + group.getButtonCount() );
//System.out.println("isSelected" + group.isSelected( after.getModel() ) + "... yup!");
//java.lang.NullPointerException is raised/thrown in this function below.
if( before.isSelected() )
{
System.out.println("1");
return 1;
}
else
{
System.out.println("2");
return 2;
}
}
}
I'm guessing this is a really easy error, yet I have been unable to find the cause. Any help would be much appreciated.