How to use Message box in place of JavaScript Alerts

I have found that JavaScript Alerts timeout after a second; as result I am trying to use the following code to display a Message box while running Katalon tests. But I have not figured out how to close the message box through Katalon. Can anyone make a suggestion as to how I can do that…

import javax.swing.JOptionPane
import javax.swing.JDialog

final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Test Message");

It’s been ages since I’ve worked with JDialog, but try this:

dialog.dispose();

Thanks for your suggestion @Brandon_Hein,
Sorry I forgot to mention that I did try using dialog.dispose(). But it did not work.

import javax.swing.JOptionPane
import javax.swing.JDialog

final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Test Message");
Thread.sleep (5000);
dialog.dispose();

Hi @Brandon_Hein, I found a solution that works. Posting it here to share with others.

import javax.swing.JDialog
import javax.swing.JOptionPane

JOptionPane jop = new JOptionPane();
jop.setMessageType(JOptionPane.PLAIN_MESSAGE);
jop.setMessage("Hello World");
JDialog dialog = jop.createDialog(null, "KatalonAlertMsg");

// Set a 5 second timer
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (Exception e) {
}
dialog.dispose();
}
}).start();
dialog.setVisible(true);