I am a new stack user and brand new to Java, so apologies in advance if this is in anyway unclear or the wrong terminology is used.
I am looking to assign a base value if no selection occurs to a JRadioButton.
i can set a JRadioButton to true which will auto select but wont appear with the listener as there was no event?
i can set an else statement, but nothing occurs again due to it waiting on a listener event?
The following code appears to work with me setting a value to the double first.
My question is, though i get the correct value assigned to variable $leadtime the system output returns its value 4 times, can someone please explain this event?
Or if there is a better way to do this i would be more than happy to learn, or perhaps i overlooked something with the two bullet point attempts?
thanks, weekendwarrior84
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JRadioButton;
public class CRADIOHAND implements ActionListener{
JRadioButton $test1;
JRadioButton $test2;
JRadioButton $test3;
JRadioButton $test4;
double $leadtime = 22;
CRADIOHAND(JRadioButton $buttonJR1,JRadioButton $buttonJR2,JRadioButton $buttonJR3,JRadioButton $buttonJR4){
$test1 = $buttonJR1;
$test2 = $buttonJR2;
$test3 = $buttonJR3;
$test4 = $buttonJR4;
System.out.println($leadtime);
}
public void actionPerformed(ActionEvent re1) {
if($test1.isSelected()){
$leadtime= 22;
}else if($test2.isSelected()){
$leadtime= 47;
}else if($test3.isSelected()){
$leadtime= 113;
}else if($test4.isSelected()){
$leadtime= 130;
}
System.out.println($leadtime);
}
}
Shortened layout.
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class CLAYOUT extends JFrame {
private FlowLayout $lay;
public CLAYOUT(){
super("Sample Program");
$lay = new FlowLayout();
setLayout($lay);
JRadioButton $buttonJR1 = new JRadioButton("22 Days");
add($buttonJR1);
JRadioButton $buttonJR2 = new JRadioButton("47 Days");
add($buttonJR2);
JRadioButton $buttonJR3 = new JRadioButton("113 Days");
add($buttonJR3);
JRadioButton $buttonJR4 = new JRadioButton("130 Days");
add($buttonJR4);
ButtonGroup radiogroup = new ButtonGroup();
radiogroup.add($buttonJR1);
radiogroup.add($buttonJR2);
radiogroup.add($buttonJR3);
radiogroup.add($buttonJR4);
$buttonJR1.addActionListener(new CRADIOHAND($buttonJR1, $buttonJR2, $buttonJR3, $buttonJR4));
$buttonJR2.addActionListener(new CRADIOHAND($buttonJR1, $buttonJR2, $buttonJR3, $buttonJR4));
$buttonJR3.addActionListener(new CRADIOHAND($buttonJR1, $buttonJR2, $buttonJR3, $buttonJR4));
$buttonJR4.addActionListener(new CRADIOHAND($buttonJR1, $buttonJR2, $buttonJR3, $buttonJR4));
}
}
Shortened Main
import javax.swing.JFrame;
public class CREORDER{
public static void main (String[] args){
CLAYOUT $clay = new CLAYOUT();
$clay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
$clay.setSize(1200,500);
$clay.setVisible(true);
}
}