Daniel Fntic
Daniel Fntic

Reputation: 3

Java - JToggleButton - While endless Loop - Cant click button twice

I need Please help. I have a Code in Java where I have a little GUI. I cant click the Button twice. I can onlx activate the while loop but i cant deactivate it.

Here is my CODE:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Test2 {

public static void main(String[] args) {

    JFrame Frame = new JFrame("");
    Frame.setBounds(100, 100, 450, 300);
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.setVisible(true);

    JPanel Panel = new JPanel();
    Frame.add(Panel);

    JToggleButton toggle = new JToggleButton("");
    Panel.add(toggle);
    toggle.setPreferredSize(new Dimension(50,50));
    toggle.addActionListener(new ActionListener() {


        public void actionPerformed(ActionEvent arg0) {
            int zufallszahl;
            if(toggle.isSelected()) {
                while(toggle.isSelected())      {

                zufallszahl = (int)(Math.random() * 10 +1);
                System.out.println(zufallszahl);
                }

            }               
        }
    }); 
}

}

Upvotes: 0

Views: 410

Answers (2)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

Your problem is here:

            while(toggle.isSelected())      {

                zufallszahl = (int)(Math.random() * 10 +1);
                System.out.println(zufallszahl);
            }

As I understand you want to generate random numbers while the button selected. To implement it you either need a SwingWorker or a Timer.

Here is an example with a Timer:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.Timer;

public class Test2 {

    public static void main(String[] args) {

        JFrame frame = new JFrame("");
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        frame.add(panel);

        final Timer timer = new Timer(300, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int zufallszahl = (int)(Math.random() * 10 +1);
                System.out.println(zufallszahl);
            }
        });
        final JToggleButton toggle = new JToggleButton("Go");
        panel.add(toggle);
        toggle.addActionListener(new ActionListener() {


            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (toggle.isSelected()) {
                    timer.restart();
                } else {
                    timer.stop();
                }
            }
        }); 
    }

}

Upvotes: 2

Giaur7
Giaur7

Reputation: 1

Your loop will stop if You'll unselect it. Better create some boolean run=true; outside actionPerformed. Then put its value in while loop instead of .isSelected(); Also delete if statement as it's repeated by while-do loop. You woul need it if You want to use do-while loop.

Upvotes: 0

Related Questions