开发者

how to create a auto closing messagebox in java

i want to create a messagebox which will be automatically close without users input. i tried reading couple of java tutorials. its a timer class. but i couldnt find the code examples.

开发者_如何学C

can someone please help me to do it. a small code example would be great


Something along these lines:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AutoDismiss implements ActionListener
{
    private JDialog dialog;

    private AutoDismiss(JDialog dialog)
    {
        this.dialog = dialog;
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent)
    {
        dialog.dispose();
    }

    public static void showMessageDialog(Component parentComponent,
                                         String message, String title,
                                         int delayInMilliseconds)
    {
        final JOptionPane optionPane = new JOptionPane(message);
        final JDialog dialog = optionPane.createDialog(parentComponent, title);
        dialog.setTitle(title);
        Timer timer = new Timer(delayInMilliseconds, new AutoDismiss(dialog));
        timer.setRepeats(false);
        timer.start();
        if (dialog.isDisplayable())
        {
            dialog.setVisible(true);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜