简体   繁体   中英

Loader on button click in java swing

I want to show loader icon on button click event in Java Swing when the event starts and loader needs to be disappeared when the event is finished.

How to do achieve this ? Currently I am doing something like this.

package swing;

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

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class LoaderDisplay {

    public static void main(String[] args) {

        final JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        JButton button1 = new JButton();

        ImageIcon loading = new ImageIcon("loading_icon.gif");
        final JLabel label = new JLabel("Loading... ", loading, JLabel.CENTER);

        label.setVisible(false);
        frame.add(panel);
        panel.add(button1);
        panel.add(label);
        frame.setVisible(true);

        button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                // event start
                label.setVisible(true);

                // some operation
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // event end
                label.setVisible(false);
            }
        });

    }

} 

Any Help ?

Basically, you should never do anything that would block the Event Dispatching Thread.

This will prevent you UI from been updated or the user interacting with it, normally leading the user to thinking that the application has died.

Instead, you should overload all time consuming or blocking tasks to a background thread.

The simplest solution would be use a SwingWorker . This provides simple to use methods for resyncing with EDT.

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LoaderDisplay {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }


                final JFrame frame = new JFrame();
                JPanel panel = new JPanel();

                JButton button1 = new JButton("Load me up");

//        ImageIcon loading = new ImageIcon("loading_icon.gif");
                final JLabel label = new JLabel("Loading... ", JLabel.CENTER);

                label.setVisible(false);
                frame.add(panel);
                panel.add(button1);
                panel.add(label);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                button1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        // event start
                        label.setVisible(true);

                        new SwingWorker<Void, String>() {
                            @Override
                            protected Void doInBackground() throws Exception {
                                // Worken hard or hardly worken...
                                Thread.sleep(2000);
                                return null;
                            }

                            @Override
                            protected void done() {
                                label.setVisible(false);
                            }
                        }.execute();

                    }
                });

            }
        });
    }
}

As a side note, you should never create, modify or interact with any UI component from any thread other then the EDT.

Check out Concurrency in Swing for more details

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