简体   繁体   中英

How do I stop a timer's actionPerformed() method?

I'm implementing a javax.swing.timer to call a synchronous operation on a GUI. However, that operation takes some time to finish and I'd like to provide an abort option to that timer call. Is that possible?

Thx!

import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Iterator;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import org.omg.CosNaming.IstringHelper;


public class vtm extends JPanel implements ActionListener{

    private JFrame frame;
    private String children[];
    private ConsoleApp hawk[];
    public static String data[][];
    public Timer timer;
    private int count1, count2;
    private int i;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    vtm window = new vtm();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public vtm() {
        timer = new Timer(25000, this);
        //timer.setInitialDelay(190);
        timer.start();
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        frame = new JFrame("VTM");
        frame.setSize(1024, 720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setIcon(new ImageIcon("config.png"));
        lblNewLabel_1.setBounds(970, 0, 51, 55);
        frame.getContentPane().add(lblNewLabel_1);
        lblNewLabel_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                config x = new config();
                x.main(null);
                timer.stop();
                frame.dispose();
            }
            });

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setIcon(new ImageIcon("bg.png"));
        lblNewLabel.setBounds(0, 0, 1024, 720);
        frame.getContentPane().add(lblNewLabel);


        String dir = System.getProperty("user.dir");

        File df = new File(dir);

        FilenameFilter fi = new FilenameFilter(){
            public boolean accept(File df, String name){
                return name.startsWith("D-");
            }
        };

        children = df.list(fi);
        hawk=new ConsoleApp[children.length];

        EventQueue.invokeLater(new Runnable(){
            public void run(){

                int count1 = 0; count2 = 0;

                for(i=0;i<children.length;i++)
                {


                hawk[i]=new ConsoleApp(children[i]);

                Iterator<String> it = hawk[i].p.iterator();

                System.out.println("Hi:"+hawk[i].p.size());

                while(it.hasNext())
                {
                    hawk[i].call(it.next());

                    if(count1 == 3)
                    {
                        count2++;
                        count1=0;
                    }

                    System.out.println("Look: " + hawk[i].l.getText().length());
                    hawk[i].l.setBounds((60 + 270*count1) + ((260-(hawk[i].l.getText().length()*7))/2), (70+(count2*60)), 260, 62);
                    hawk[i].c.setBounds(50 + 270*count1, (70+(count2*60)), 273, 62);
                    frame.getContentPane().add(hawk[i].c,0);
                    frame.getContentPane().add(hawk[i].l,0);

                    count1++;
                }
            }
            }
        });
    }

    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("lol");

        EventQueue.invokeLater(new Runnable(){
                public void run(){

                    int count1 = 0; count2 = 0;

                    for(i=0;i<children.length;i++)
                    {


                    hawk[i]=new ConsoleApp(children[i]);

                    Iterator<String> it = hawk[i].p.iterator();

                    System.out.println("Hi:"+hawk[i].p.size());

                    while(it.hasNext())
                    {
                        hawk[i].call(it.next());

                        if(count1 == 3)
                        {
                            count2++;
                            count1=0;
                        }

                        hawk[i].l.setBounds((60 + 270*count1) + ((260-(hawk[i].l.getText().length()*7))/2), (70+(count2*60)), 260, 62);
                        hawk[i].c.setBounds(50 + 270*count1, (70+(count2*60)), 273, 62);
                        frame.getContentPane().add(hawk[i].c,0);
                        frame.getContentPane().add(hawk[i].l,0);

                        count1++;
                    }
                }
                }
            });


    }


}

You actionPerformed triggers a Runnable, which I assume is what takes a long time and what you would like to abort. Inside the Runnable's run method you have two loops, for(i=0;i<children.length;i++) and while(it.hasNext()) . If you add a stopping condition to these loops, you can make the Runnable finish early by controlling that condition from outside.

For example, add a field abort to vtm and change the two loops to for(i=0;i<children.length && !abort;i++) and while(it.hasNext() && !abort) respectively. You can now set abort to true and the Runnable will stop the next time it tries to loop again.

Use a Swingworker instead of your Runnable, this is how you implement a canceling mechanism: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/cancel.html

Also see this question: How do I use SwingWorker in Java?

I'm implementing a javax.swing.timer to call a synchronous operation on a GUI. However, that operation takes some time to finish and I'd like to provide an abort option to that timer call. Is that possible?

If you want to provide UI to your user to cancel that Runnable which you posted on the Event Dispatch Thread (EDT) while it is running, then no, it is not possible.

The moment your Runnable code starts executing, your UI will be unresponsive as the UI thread (the EDT) is occupied with your Runnable . So any Cancel button or whatever interface you had in mind will not react on any clicks until the Runnable is finished.

You can cancel it before it starts running, but a better investment of your time might be to investigate whether you really have to perform heavy work on the EDT (for which normally the answer is no). It would be better to do all the heavy calculations/operations on a worker Thread , and update the UI during/afterwards. The SwingWorker class was designed for this, and many examples of its usage can be found online (in the javadoc, the official Swing tutorials and on this site)

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