1

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You haven't registered `ActionListener` anywhere. To which component have you registered `ActionListener`? Show your complete code.. – Vishal K Mar 16 '13 at 09:15
  • Hi Vishal K, I have edited my post to show the layout/main which shows where i have registered ActionListener against these JRadio Buttons hopefully correctly.. – weekendwarrior84 Mar 16 '13 at 10:39

1 Answers1

0

The click on RadioButton is indeed printing the result only one time. But the initial four times printing of 22.0 is because of this line that you have written in your constructor CRADIOHAND.

System.out.println($leadtime);

Because of this , on each call to constructor of CRADIOHAND in CLAYLAYOUT at:

        $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));

The SOP within constructor of CRADIOHAND is calling 4 times in rows. So the solution is that you comment or remove System.out.println($leadtime); in CRADIOHAND constructor.

NOTE: As a side note. I want to suggest you to stick with java naming convention while writing your codes. For Example: A class name always starts with a capital letter. A variable name always starts with small letter. And the constants (i.e Final variables) are having its all letters in capital. There are many others. Have a look at the official site of oracle.

Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • Thanks for your answer Vishal K. I did notice and mention this line in my edit but was unaware it was due to the multiple calls to the constructor. Also thanks for the link, i was trying to put my own spin to separate my language from Java, but with this link there is no need to reinvent the wheel! Question Answered. – weekendwarrior84 Mar 16 '13 at 12:17