How to create automatic message box closure in java

I want to create a mailbox that will be automatically closed without user input. I tried to read a couple of java lessons. its timer class. but I could not find code examples.

Can someone please help me do this. a small code example would be great

+4
source share
1 answer

Something like that:

public class AutoDismiss implements Runnable, ActionListener { private JDialog dialog; public AutoDismiss(JDialog dialog) { this.dialog = dialog; } @Override public void actionPerformed(ActionEvent e) { dialog.dispose(); } // EDIT: in response to comment static public void showMessageDialog(Component parent, Object message) { // run all of this on the EDT final JOptionPane optionPane = new JOptionPane(message); String title = UIManager.getString("OptionPane.messageDialogTitle"); int style = styleFromMessageType(JOptionPane.INFORMATION_MESSAGE); final JDialog dialog = optionPane.createDialog(parent, title, style); Timer timer = new Timer(5000, new AutoDismiss(dialog)); timer.setRepeats(false); timer.start(); if (dialog.isDisplayable()) dialog.setVisible(true); } } 
+3
source

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


All Articles