How to pause a program until a button is clicked?

I use a class extended from jframe and it has a button (I use it in my program)

I want that when I run jframe in my program, all my pause in the program

until I press the button.

How can i do this

in C ++ getch() do this.

I need such a function.

+4
source share
6 answers

Pausing sleep execution , although I doubt that this is the mechanism you want to use. Therefore, like others, I believe that you need to implement the wait-notify . Here's a very contrived example:

 import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.concurrent.atomic.AtomicBoolean; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; @SuppressWarnings("serial") public class PanelWithButton extends JPanel { // Field members private AtomicBoolean paused; private JTextArea textArea; private JButton button; private Thread threadObject; /** * Constructor */ public PanelWithButton() { paused = new AtomicBoolean(false); textArea = new JTextArea(5, 30); button = new JButton(); initComponents(); } /** * Initializes components */ public void initComponents() { // Construct components textArea.setLineWrap(true); textArea.setWrapStyleWord(true); add( new JScrollPane(textArea)); button.setPreferredSize(new Dimension(100, 100)); button.setText("Pause"); button.addActionListener(new ButtonListener()); add(button); // Runnable that continually writes to text area Runnable runnable = new Runnable() { @Override public void run() { while(true) { for(int i = 0; i < Integer.MAX_VALUE; i++) { if(paused.get()) { synchronized(threadObject) { // Pause try { threadObject.wait(); } catch (InterruptedException e) { } } } // Write to text area textArea.append(Integer.toString(i) + ", "); // Sleep try { Thread.sleep(500); } catch (InterruptedException e) { } } } } }; threadObject = new Thread(runnable); threadObject.start(); } @Override public Dimension getPreferredSize() { return new Dimension(400, 200); } /** * Button action listener * @author meherts * */ class ButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent evt) { if(!paused.get()) { button.setText("Start"); paused.set(true); } else { button.setText("Pause"); paused.set(false); // Resume synchronized(threadObject) { threadObject.notify(); } } } } } 

And here is your main class:

  import javax.swing.JFrame; import javax.swing.SwingUtilities; public class MainClass { /** * Main method of this application */ public static void main(final String[] arg) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new PanelWithButton()); frame.pack(); frame.setVisible(true); frame.setLocationRelativeTo(null); } }); } } 

As you can see, this sample application will constantly write to the text area until you click the button that reads “Pause”, after which you will need to click the same button that will now read “Start”.

+10
source

This answer depends entirely on whether I understood your question correctly, please give a little more information if you want to get better answers. Here:

Pause in loop script

  boolean paused; while(true ) { if(paused) { Thread.sleep(1000); // or do whatever you want in the paused state } else { doTask1 doTask2 doTask3 } } 

Topics: You can also put these tasks in a separate thread, rather than in a GUI thread that is commonly used for long-running operations.

Pausing a thread is very simple. Just call the suspend () function. If you want to pause the resume () call. However, these methods are dangerous and outdated. A better or rather safer way to do this would be similar to the one above by setting the pause flag. Here is a brief example that I lay in my fragments. I can’t remember exactly where I got it in the first place:

 // Create and start the thread MyThread thread = new MyThread(); thread.start(); while (true) { // Do work // Pause the thread synchronized (thread) { thread.pleaseWait = true; } // Do work // Resume the thread synchronized (thread) { thread.pleaseWait = false; thread.notify(); } // Do work } class MyThread extends Thread { boolean pleaseWait = false; // This method is called when the thread runs public void run() { while (true) { // Do work // Check if should wait synchronized (this) { while (pleaseWait) { try { wait(); } catch (Exception e) { } } } // Do work } } } // Create and start the thread MyThread thread = new MyThread(); thread.start(); while (true) { // Do work // Pause the thread synchronized (thread) { thread.pleaseWait = true; } // Do work // Resume the thread synchronized (thread) { thread.pleaseWait = false; thread.notify(); } // Do work } class MyThread extends Thread { boolean pleaseWait = false; // This method is called when the thread runs public void run() { while (true) { // Do work // Check if should wait synchronized (this) { while (pleaseWait) { try { wait(); } catch (Exception e) { } } } // Do work } } } 

Hope this helps

+1
source

try my java pause button:

 package drawFramePackage; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.Timer; public class Milliseconds2 implements ActionListener, MouseListener{ JFrame j; Timer t; Integer onesAndZeros, time, time2, placeHolder2; Boolean hasFired; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Milliseconds2(); } public Milliseconds2(){ j = new JFrame(); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); j.setSize(new Dimension(300, 300)); j.setVisible(true); j.addMouseListener(this); onesAndZeros = new Integer(0); time = new Integer(0); time2 = new Integer(0); placeHolder2 = new Integer(0); hasFired = new Boolean(true); t = new Timer(2400, this); time = (int) System.currentTimeMillis(); t.start(); } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub if (onesAndZeros.equals(0)){ t.stop(); if (hasFired){ time2 = t.getDelay() - ((int) System.currentTimeMillis() - time); } else{ time2 -= (int) System.currentTimeMillis() - placeHolder2; } if (hasFired){ hasFired = false; } onesAndZeros = -1; } if (onesAndZeros.equals(1)){ //System.out.println(time2); t.setInitialDelay(time2); t.start(); placeHolder2 = (int) System.currentTimeMillis(); onesAndZeros = 0; } if (onesAndZeros.equals(-1)){ onesAndZeros = 1; } } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub time = (int) System.currentTimeMillis(); hasFired = true; System.out.println("Message"); } } 
+1
source

Freezing the main theme will effectively freeze the entire program and may cause the operating system to think that the application crashed; I’m not quite sure if you correct me if I am mistaken. You can try to hide / disable the controls and turn them on again when the user clicks on your button.

0
source

You do not say what you mean by a pause. What does your application do?

Generally, you CANNOT pause the user interface application. User interface applications are launched from the message processing loop. A message arrives, a message is sent, the loop waits for another message. The application still needs to handle things like clicking on buttons, resizing the window, closing the application, etc., so that this cycle runs continuously.

If you want your application to “pause” in the sense that the user wasn’t doing something, just gray outside any button or menu that you don’t want users to do.

If your application starts a thread in the background and wants to pause this action until it resumes, you can do it quite easily, like this.

 MyThread mythread = new MyThread(); // Main thread void pause() { mythread.pause = true; } void resume() { synchronized (mythread) { mythread.pause = false; mythread.notify(); } } class MyThread extends Thread { public boolean pause = false; public void run() { while (someCondition) { synchronized (this) { if (pause) { wait(); } } doSomething(); } } } 

You can also use Thread.suspend (), Thread.resume () to achieve similar ones, but they are inherently dangerous because you don’t know where this thread is when you pause it. He can open the file, be halfway through sending a message through a socket, etc. Putting a test into any cycle of controlling your flow allows you to pause the action at a time when it is safe.

0
source

The user interface performs the task using a message-driven mechanism.

If you have a button in the user interface, and you want to start something when you click this button, you must add an ActionListener button to it. After clicking the button, it starts the ActionListener object to complete the task, for example:

 button.addActionListener(new ActionListener { @Override public void actionPerformed(ActionEvent e) { // do something } }); 

If you want to stop something when you press the pause button, you will need Thread . This is more complicated than in the first case.

0
source

Source: https://habr.com/ru/post/1345710/


All Articles