Alex
Alex

Reputation: 970

Two ways of using JOptionPane classes from Swing

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?

Upvotes: 2

Views: 1129

Answers (1)

Sujay
Sujay

Reputation: 6783

The second option would be preferred because you really don't need to create a JOptionPane object when you're trying to merely project a MessageDialog to 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

Upvotes: 3

Related Questions