Centering Dialog Using JOptionPane.showMessageDialog

To always display the dialogs, I always used code similar to the following:

JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(this), … yada

However, now I think this is "more correct":

JOptionPane.showMessageDialog(getRootPane(), … yada

What do people prefer / recommend?

+3
source share
2 answers

None of them are β€œmore correct” - it just depends on what effect you want. From JOptionPane JavaDocs -

parentComponent: , . : , , , . . , , ( L & F).

, - JOptionPane.getFrameForComponent() SwingUtilities.getWindowAncestor() ; getRootPane() RootPaneContainer ( , , JFrame), "this" .

+3

JFrame JDialog, , this :

JOptionPane.showMessageDialog(this, ...);
+1

Source: https://habr.com/ru/post/1714959/


All Articles