javax.swing.JOptionPane
Here is the code for the method that I call whenever I want the info window to pop up, it launches the screen until it is accepted:
import javax.swing.JOptionPane; public class ClassNameHere { public static void infoBox(String infoMessage, String titleBar) { JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + titleBar, JOptionPane.INFORMATION_MESSAGE); } }
The first parameter, JOptionPane ( null in this example), is used to align the dialog. null forces it to center itself on the screen, however any java.awt.Component can be specified, and instead a dialog will appear in the center of this Component .
I usually use the titleBar String to describe where in the code from which the box is called, so if it's annoying, I can easily track and remove the code responsible for sending my screen using infoBoxes.
To use this method call:
ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE");
javafx.scene.control.Alert
For a detailed description of how to use JavaFX dialogs, see JavaFX Dialogs (official) using code.makery. They are much more powerful and flexible than Swing dialogs, and are capable of much more than just pop-up messages.
As above, I will post a small example of how you could use JavaFX dialogs to achieve the same result
import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.application.Platform; public class ClassNameHere { public static void infoBox(String infoMessage, String titleBar) { infoBox(infoMessage, titleBar, null); } public static void infoBox(String infoMessage, String titleBar, String headerMessage) { Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle(titleBar); alert.setHeaderText(headerMessage); alert.setContentText(infoMessage); alert.showAndWait(); } }
Keep in mind that JavaFX is a single-threaded set of GUI tools, which means that this method should be called directly from the JavaFX application thread. If you have another thread doing work that needs a dialog, see These SO Q & As: JavaFX2: Can I pause a background job / service? and Platform and Javafx .
To use this method call:
ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE");
or
ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE", "HEADER MESSAGE");