Experimenting with Swing, I learnt that there are two ways of using JOptionPane and other classes from Swing:
1) Declare
private JOptionPane info1 = new JOptionPane();
before the class constructor and then use info1.showMessageDialog() in the relevant method (in this case I get message that showMessageDialog should be accessed in a static way) , or
2) In the relevant method use
JOptionPane.showMessageDialog()
without declaring the object of JOPtionPane class at all.
My question is, what are the differences, drawbacks and benefits of these two approaches? Does it extend to other Swing classes?
The second option would be preferred because you really don’t need to create a
JOptionPaneobject when you’re trying to merely project aMessageDialogto the user.In fact if you open the tutorial: “How to Make Dialogs”, this option happens to be the suggested mechanism to handle dialogs in
Swing