Home > Java Core > Android Toast like message dialog in Java Swing

Android Toast like message dialog in Java Swing


Android Toast like message dialog in Java Swing

If you are using SwingWorker background thread for doing long operations and if you want user to see some useful information then ofcourse you need to use process() function as it is invoked by Event Dispatcher Thread. But if you using JOptionPane.showMessageDialog(null, “message”); you can invoke message dialog from background worker thread. But this will block the background worker thread until user provides his/her input (i.e. close the dialog or click on OK). But here I provide you a solution for this, we will place the message dialog as Android Toast information, i.e. our message dialog will close automatically after a certain delay. For creating delay we will use Timer class of Swing framework.

final int TIME_VISIBLE = 3000; //in milliseconds
JOptionPane pane = new JOptionPane("your message", JOptionPane.INFORMATION_MESSAGE);
final JDialog dialogErr = pane.createDialog(null, "Title");
new Timer(TIME_VISIBLE, new ActionListener() {
	public void actionPerformed(ActionEvent e) {

About jkoder