How to present a simple alert message in java?

JavaSwingJoptionpane

Java Problem Overview


Coming from .NET i am so used calling Alert() in desktop apps. However in this java desktop app, I just want to alert a message saying "thank you for using java" I have to go through this much suffering:

(using a JOptionPane)

Is there an easier way?

Java Solutions


Solution 1 - Java

I'll be the first to admit Java can be very verbose, but I don't think this is unreasonable:

JOptionPane.showMessageDialog(null, "My Goodness, this is so concise");

If you statically import javax.swing.JOptionPane.showMessageDialog using:

import static javax.swing.JOptionPane.showMessageDialog;

This further reduces to

showMessageDialog(null, "This is even shorter");

Solution 2 - Java

Assuming you already have a JFrame to call this from:

JOptionPane.showMessageDialog(frame, "thank you for using java");

See The Java Tutorials: How to Make Dialogs
See the JavaDoc

Solution 3 - Java

If you don't like "verbosity" you can always wrap your code in a short method:

private void msgbox(String s){
   JOptionPane.showMessageDialog(null, s);
}

and the usage:

msgbox("don't touch that!");

Solution 4 - Java

Even without importing swing, you can get the call in one, all be it long, string. Otherwise just use the swing import and simple call:

JOptionPane.showMessageDialog(null, "Thank you for using Java", "Yay, java", JOptionPane.PLAIN_MESSAGE);

Easy enough.

Solution 5 - Java

Call "setWarningMsg()" Method and pass the text that you want to show.

exm:- setWarningMsg("thank you for using java");


public static void setWarningMsg(String text){
	Toolkit.getDefaultToolkit().beep();
	JOptionPane optionPane = new JOptionPane(text,JOptionPane.WARNING_MESSAGE);
    JDialog dialog = optionPane.createDialog("Warning!");
    dialog.setAlwaysOnTop(true);
    dialog.setVisible(true);
}

Or Just use

JOptionPane optionPane = new JOptionPane("thank you for using java",JOptionPane.WARNING_MESSAGE);
JDialog dialog = optionPane.createDialog("Warning!");
dialog.setAlwaysOnTop(true); // to show top of all other application
dialog.setVisible(true); // to visible the dialog

You can use JOptionPane. (WARNING_MESSAGE or INFORMATION_MESSAGE or ERROR_MESSAGE)

Solution 6 - Java

Can use this code simply like javascript:

void alert(String str){
        JOptionPane.showMessageDialog(null, str);
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaoestView Question on Stackoverflow
Solution 1 - JavaDónalView Answer on Stackoverflow
Solution 2 - JavaBill the LizardView Answer on Stackoverflow
Solution 3 - JavaJoe CottonView Answer on Stackoverflow
Solution 4 - JavaOnResolveView Answer on Stackoverflow
Solution 5 - JavaMahfuz AhmedView Answer on Stackoverflow
Solution 6 - Javabahoz99View Answer on Stackoverflow