1

I'd like to change value of my variable "name" when I select right button and click "ok" on my JRadio Frame.

For example when i select r1 and hit "ok" I'd like to have name=="Fast" in the entire package.

package Snake;
public class Radio extends JFrame {
    private int delay = 100;
    private String name;
    JTextField t1;
    JButton b;
    JRadioButton r1, r2;
    JLabel l;

    public void selectSpeed() {

        b = new JButton("Ok");
        r1 = new JRadioButton("Fast");
        r2 = new JRadioButton("Slow");
        l = new JLabel("Speed: ");

        ButtonGroup bg = new ButtonGroup();
        bg.add(r1);
        bg.add(r2);
        add(b);
        add(r1);
        add(r2);
        add(l);

        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                if (r1.isSelected()) {
                    name = "Fast";
                } else {
                    name = "Slow";
                }
                l.setText("Speed: " + name); // name=="Fast" when r1 is selected
            } // name=="Slow" when r2 is selected
        });
        if (name == "Fast") { // and now name is empty...
            delay = 50;

        }
        if (name == "Slow") {
            delay = 500;
        }

        setLayout(new FlowLayout());
        setSize(400, 400);
        setVisible(true);
    }

    public int setSpeed() {
        selectSpeed();
        return delay;

    }

}
  • 4
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Ousmane D. Apr 30 '17 at 17:51

3 Answers3

0

You need to do it in your JRadioButton listener. For example, like here, at first you change the variable "name" and later in the current listener you check conditions, but you need remember that to compare the strings you need to use "equals":

    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (r1.isSelected()) {
                name = "Fast";
            } else {
                name = "Slow";
            }
            l.setText("Speed: " + name); // name=="Fast" when r1 is selected

            if (name.equals("Fast")) { // and now name is empty...
                delay = 50;

            }
            if (name.equals("Slow")) {
                delay = 500;
            }
        } // name=="Slow" when r2 is selected
    });
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
0

If you want to change the delay on button click, You need to write the logic in the ActionListener itself because the code you have written to change the delay will run only once and that too at the start of the execution of your program and at that time, name will be empty.

Then when ever you click the button, It will only execute the ActionListener So delay will not be changed at any time. And other mistake you are making is that you are comparing Strings in wrong way. For more information take a look at it How do I compare Strings in Java?

To change delay dynamically on button click, you need to change it in the ActionListener.

 b.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {

        if (r1.isSelected()) {
            name = "Fast";
            delay = 50;
        } else {
            name = "Slow";
            delay = 500;
        }
        l.setText("Speed: " + name); // name=="Fast" when r1 is selected
    } // name=="Slow" when r2 is selected
});
Community
  • 1
  • 1
Sanket Makani
  • 2,491
  • 2
  • 15
  • 23
0

Well I see my mistake now, Thank you.

But it still does not work the way I like. I'd like to change the "delay" value every time I select right button on JRadio and hit "ok" and with this changed value I'd like to go to the other class.

There is the code of a class where I need value of "delay":

package Snake;
public class Gameplay extends Paint implements KeyListener, ActionListener {
private Timer timer;
private int q = 0;
Radio radio = new Radio();


public Gameplay() {

    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    timer = new Timer(radio.selectSpeed(), this); //here i need flexible "delay" value
    timer.start();
}