简体   繁体   中英

java Swing: How to pass value by pressing one button while other button is already pressed

I am working on a project with Java swing and Matlab. I have a GUI with 2 buttons "Run" and "Pause". I am using one more java program (Matlabjavaprog.java) where I am running a loop as follows:

int pause = 0;
for (int i=0; i<10; i++) {
    if (pause == 20000) {
        try { 
            Thread.sleep(pause); 
            System.out.println("Now delayed for 20s!");
        } catch (InterruptedException ie) {} 
    } else {
        proxy.setVariable("n", i);
        proxy.eval("n=n+1");
        proxy.feval("myfun");
    }
}

When I pressed "Run" button, the else part executes. But I want to press "Pause" button in between this loop where value of pause (20000) will be passed to java program from GUI, and the execution should be delayed by 20000ms. However, I am not able to press "Pause" button until "Run" is executing the loop.

"Run" button:

JButton btnNewButton = new JButton("Run");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pauseButton.setEnabled(true);
                prog1.main(args); // a java program that calls another program Matlabjavaprog.java (which calls an instance of Matlab)
                }
        });     

"Pause" button:

pauseButton = new JButton("Pause");
        pauseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int p =20000;
                Matlabjavaprog.getpause(p); // a function in Matlabjavaprog java program that passes pause value from GUI to this program
            }
        });

Actually "Run" creates a TCP connection with MATLAB and within it it runs several instances of data set. The whole process cannot be put in thread as it will not allow to reconnect as it has already a connection. It seems that once I pressed "Run", I cannot press "Pause" until run is complete. Is there any way to execute "Pause" button that could delay the loop as per user need? Currently, I am not able to sent pause value to the running program, ie, Matlabjavaprog.java. Any help will be appreciable!

How about putting the execution in another thread by implementing Runnable and using an AtomicBoolean

eg

AtomicBoolean isPaused = new AtomicBoolean(false);
new Thread(new Runnable(){
    public void run(){
       while(true){
       if(isPaused.get()){
         Thread.sleep(20000);
         isPaused.set(false);
       }
       proxy.setVariable("n", i);
       proxy.eval("n=n+1");
       proxy.feval("myfun");
      }
   }
}).start()

Then in your pause button, call a setter that calls isPause.set(true) and that should make it clickable. It's a start anyway - I like using atomics for signaling between threads for simplicity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM