Reputation: 967
I am developing a swing application, just a little query about JOptionPane.showMessageDialog()
which is bugging me:
JOptionPane.showMessageDialog(null, "Record entered successfully");
JOptionPane.showMessageDialog(this, "Record entered successfully");
The question is: while implementing null
as the first argument i get the message at the background of current parent frame whereas if i write this
as the first argument the window comes over the parent frame. Why is this happenning?
Upvotes: 2
Views: 1451
Reputation: 13427
In the method
showMessageDialog(Component parentComponent, Object message)
the first argument sets the parent of the dialog:
parentComponent
Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be
null
, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).
I assume that the method appears inside a JFrame
class, in which case passing this
as the argument will set the parent component as that frame.
Upvotes: 1
Reputation: 57
The java keyword this
is used (in this case) to refer to the current class - so you're referring to your parent window. See this link, It's pretty handy:
http://javapapers.com/core-java/explain-the-java-this-keyword/
Upvotes: 0