Swing Dialogs

In most applications, information occasionally needs to be displayed for a brief period of time, often just long enough for the user to read it and click OK or perhaps enter a value, such as a password. Swing provides the JOptionPane class to make creating such simple dialog boxes extremely easy — in many cases requiring just one line of code.
 
Applications may also serve more complex dialog needs, such as providing a property editor in which a set of related values can be modified, with an appropriate interface. Swing’s JDialog class supports such general-purpose dialogs. JDialogs can also be non-modal,which means the user does not need to close the dialog before interacting with other application windows. When possible, implementing such an interface yields a more pleasant and productive user experience.
 
Even though JOptionPane makes it very easy (for the programmer) to pop up a dialog, bear in mind that this will disrupt the flow of activity for users and force them to deal with the dialog before they can proceed with their underlying task. While this is sometimes unavoidable or even appropriate, it is usually worth trying to find less disruptive alternatives (direct manipulation, a non-modal floating notification, or some other non-modal approach). This may require more work on the part of the developer but will result in a better application. And if the application is widely adopted, the benefits are multiplied across the entire user base.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JOptionDialog {
	public static void main(String[] args) {
		JFrame f = new JFrame();
		Container c = f.getContentPane();
		c.setLayout(new BorderLayout());
		JOptionPane.showInputDialog(null, "Please choose a name", "Example 1",
				  JOptionPane.QUESTION_MESSAGE, null, new Object[] {
				    "Amanda", "Colin", "Don", "Fred", "Gordon", 
                                    "Janet", "Jay","Joe","Judie", "Kerstin", 
                                    "Lotus", "Maciek", "Mark", "Mike",
                                    "Mulhern","Oliver", "Peter", "Quaxo", "Rita",
                                    "Sandro","Tim", "Will"}, "Joe");

		JPanel p = new JPanel(new FlowLayout());
		//p.add(op);
		c.add(p);
		c.add(new JLabel("Example 6", JLabel.CENTER), BorderLayout.NORTH);
		f.setSize(200,200);
		f.setVisible(true);
	}
}

Here’s another input dialog. This time, we don’t provide any selection values, so we get a JTextField. The default value we supply is entered in the field when it comes up .

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JOptionDialog {
	public static void main(String[] args) {
		JFrame f = new JFrame();
		Container c = f.getContentPane();
		c.setLayout(new BorderLayout());
		JOptionPane.showInputDialog(null, "Please enter your name", "Example 2",
				  JOptionPane.QUESTION_MESSAGE, null, null, "Shannon");

		JPanel p = new JPanel(new FlowLayout());
		//p.add(op);
		c.add(p);
		c.add(new JLabel("Example 6", JLabel.CENTER), BorderLayout.NORTH);
		f.setSize(200,200);
		f.setVisible(true);
	}
}