Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between frame and null in showMessageDialog in Java?

Tags:

java

swing

Say i have an object of the JFrame class as frame

I was wondering what is the difference between

JOptionPane.showMessageDialog(null,message) 

and

JOptionPane.showMessageDialog(frame,message)

to print something out.Both give the same result and they pop up at the same place.So i was wondering what is really the difference between these two? What happens actually in background differently?

like image 596
ProgrammerPotato Avatar asked Dec 04 '22 09:12

ProgrammerPotato


2 Answers

When providing the frame or any other component the option pane will pop up at the middle of the component. However if you are providing null then it will pop up at the middle of your screen.

In your case I guess your Jframe is of your screen size. So if you reduce your frame size and start it at the default location i.e top left corner then you may see the difference.

like image 163
pundit Avatar answered Jan 19 '23 00:01

pundit


// the dialog is centered on the desktop
JOptionPane.showMessageDialog(null,message)

// the dialog is centered on the frame
JOptionPane.showMessageDialog(frame,message)
like image 43
Gabriel Negut Avatar answered Jan 18 '23 23:01

Gabriel Negut