I’ve always been using code similar to the following to display dialogs:
JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(this), … yada
However, I’m now thinking that this is “more correct”:
JOptionPane.showMessageDialog(getRootPane(), … yada
What do people prefer / recommend?
Neither one is “more correct” – it just depends on what effect you want. From the JOptionPane JavaDocs –
In your case, you want the dialog to be centered on a frame – JOptionPane.getFrameForComponent(component), or SwingUtilities.getWindowAncestor(component) work from any component; getRootPane() only works from a RootPaneContainer (i.e. probably a JFrame) and you might as well just use “this” in that context.