152

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?

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
Haoest
  • 13,610
  • 29
  • 89
  • 105
  • 10
    What exactly do you expect from "easier"? If it's the verbosity, why not just wrap it in some static utility method and hide it away, for example? By the way, the official tutorial is [here](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html). – BalusC Feb 02 '12 at 20:28

6 Answers6

274

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");
Ajay
  • 437
  • 7
  • 22
Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 39
    Nothing wrong with verbosity. In fact, I consider that one of its strengths. – mre Feb 02 '12 at 20:32
  • 16
    @mre I guess this is either a joke or you're misunderstanding "verbosity"? – Dónal Feb 02 '12 at 20:34
  • 38
    @mre unless you need the typing practice to transition to a secretarial job, I don't see how verbosity is desirable. – Dónal Feb 03 '12 at 10:52
  • 7
    @Dónal When some simple thing takes a whole page of code to get working, that really creates a whole lot of extra jobs for people! :) – Evgeni Sergeev Mar 04 '15 at 07:38
  • 1
    Good answer. Anyway, I can only use the first one. The second version which is shorter doesn't work (Error on the import command: "The import cannot be resolved"). – Alisa Jun 25 '15 at 16:44
  • 6
    @Alids to use the second version, you need to add the following to your imports `import static javax.swing.JOptionPane.showMessageDialog` – Dónal Jun 25 '15 at 18:30
  • 10
    Well, one argument for verbosity could be gained from a counterexample - I've spent quite a few minutes trying to disentangle the meaning of a single compact line, before. Being brief sometimes comes at the cost of readability. Granted, verbosity doesn't automatically grant readability. – Erhannis Jul 07 '18 at 23:41
42

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

Nathan
  • 8,093
  • 8
  • 50
  • 76
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
37

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!");
Dónal
  • 185,044
  • 174
  • 569
  • 824
Joe Cotton
  • 491
  • 4
  • 5
28

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.

David Moles
  • 48,006
  • 27
  • 136
  • 235
OnResolve
  • 4,016
  • 3
  • 28
  • 50
11

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)

Mahfuz Ahmed
  • 721
  • 9
  • 23
2

Can use this code simply like javascript:

void alert(String str){
        JOptionPane.showMessageDialog(null, str);
    }
bahoz99
  • 121
  • 1
  • 6