What's the Java Swing counterpart for the JavaScript alert()
popup (if there is one)?
Asked
Active
Viewed 5,524 times
2

Bluefire
- 13,519
- 24
- 74
- 118
-
2Are you referring to a [JOptionPane](http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html)? – Hovercraft Full Of Eels Jun 16 '12 at 17:16
-
Not sure what that is... – Bluefire Jun 16 '12 at 17:18
-
Aah, thanks! Post it as an answer so I can accept it. – Bluefire Jun 16 '12 at 17:18
-
Others have given similar answers -- please upvote their answers and select one as the correct one. Much luck! – Hovercraft Full Of Eels Jun 16 '12 at 17:22
3 Answers
7
An basic way to accomplish such a popup is to use JOptionPane.showMessageDialog
, see here.
JOptionPane.showMessageDialog(frame, "here comes the text.");
// assumes frame to be a reference to the desired parent frame

Howard
- 38,639
- 9
- 64
- 83
-
How would I make that pane pop up (e.g. when a button is clicked, what code do I use to handle the event?)? – Bluefire Jun 16 '12 at 17:36
-
1@Bluefire You may attach an `ActionListener` to your `JButton`, [see here](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html). – Howard Jun 16 '12 at 17:44
2
Think you are looking for JOptionPanes but here is a good description of the different dialogs: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

barsju
- 4,408
- 1
- 19
- 24
2
How to create a dialog box in Java:
- How to Make Dialogs (The Java Tutorials)
- Java - How to create a custom dialog box? (stackoverflow answer)

Community
- 1
- 1

Christopher Peisert
- 21,862
- 3
- 86
- 117