1

I want to make a swing application that has two (or multiple) buttons , which switch between ongoing actions. To keep it simple those actions can just be a repeating print. Here is the framework for the JFrame:

public class DayNight extends JFrame implements ActionListener{

    //JFrame entities
    private JPanel animationPanel;
    private JButton button;
    private JButton button2;


    public static void main(String[] args) {
        DayNight frame = new DayNight();
        frame.setSize(2000, 1300);
        frame.setLocation(1000,350);
        frame.createGUI();
        frame.setVisible(true);
        frame.setTitle("Day/Night Cycle, Rogier");
    }

   private void createGUI() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();
    window.setLayout(new FlowLayout() );
    animationPanel = new JPanel();
    animationPanel.setPreferredSize(new Dimension(2000, 900));
    animationPanel.setBackground(Color.black);
    window.add(animationPanel);

    button = new JButton("choice1");
    button.setFont(new Font("Arial", Font.PLAIN, 50));
    window.add(button);
    button.setActionCommand("choice1");
    button.addActionListener(this);

    button2 = new JButton("choice2");
    button2.setFont(new Font("Arial", Font.PLAIN, 50));
    window.add(button2);
    button2.setActionCommand("choice2");
    button2.addActionListener(this);
   }
}

I've tried the following, but I now know this was a completely wrong approach.

public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    while ("Stop"!=(command)){
        command = event.getActionCommand();
        try{
            Thread.sleep(500);
            if ("choice1".equals(command)){
                System.out.println("choice1");
            }
            else if("choice2".equals(command)){
                System.out.println("choice2");
            }
            else{
                System.out.println("no choice");
            }
        }   
        catch(InterruptedException ex){
            Thread.currentThread().interrupt();
        }

    }
}

I've since read many posts and the "concurrency in swing" doc from Java Tutorials. I now understand what Idid wrong but I still have no idea how to get started. My (sub)goal is to have the program print "choice1" (or any other action) continueously, and to have it switch to printing "choice2" after I press the other button. Can someone give me a little push in the right direction?

RnRoger
  • 682
  • 1
  • 9
  • 33
  • Quit reposting questions. Keep all the information in one thread so everybody knows what has already been suggested. – camickr Sep 14 '17 at 14:49
  • I re-valuated from both the answers I got here and on reddit. I can't continue on my old question because then people get mad and tell me to "make a new question". They then tell me to not post a new question. Happened before, jsut skipping a step. – RnRoger Sep 14 '17 at 15:16
  • `They then tell me to not post a new question` - this is NOT a new question. That is why all information should be in the same question. The answer here (to use a separate Thread) is the same as the answer in your last question, because the question is the same. – camickr Sep 14 '17 at 15:22

1 Answers1

0

You will most probably need to start Thread separately. Then, you can create a button/actionlistener for each. It gets much harder to have the threads output something on the same widget, so let's say each Thread prints on the console:

class SampleThread extends Thread {
    private String myOutput;
    public boolean paused = true;
    public SampleThread(String output) { myOutput = output; }
    public void run() {
        while (true) {
            if (!paused) {
                System.out.println(myOutput);
            }
            Thread.sleep(100);
        }
    }
    public void setPaused(boolean p) { paused = p; }
}

Note that this is just something you can start without a GUI:

public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        SampleThread t = new SampleThread("hello from thread " + i);
        t.setPaused(false);
        t.start();
    }
}

Now for the Swing control, you can create a button with an action listener for each:

Colletion<SampleThread> threads = new ArrayList<>(); // all threads
public void init(int count) {
    for (int i = 0; i < count; i++) {
        JButton button = new JButton("activate thread " + i);
        String output = "thread " + i;
        SampleThread thread = new SampleThread(output);
        thread.start();
        threads.add(thread);
        button.addActionListener() {
            public void actionPerformed(ActionEvent e) {
                // pause all threads except the one we just created
                threads.forEach(t -> t.setPaused(t != thread));
            }
        }
        add(button);
    }
}
daniu
  • 14,137
  • 4
  • 32
  • 53