How to add to Dialog Boxes in Java (Swing)? [duplicate]
Possible Duplicate:
Simple popup java form with at least two fields
I'm trying to make a Dialog Box that has multiple buttons, places to enter text, etc. I'm using this tutorial - http://download.oracle.co开发者_开发知识库m/javase/tutorial/uiswing/components/dialog.html, but it doesn't have anything about having multiple items. Is there a way to do this?
Many of the showXXXXDialog methods take an Object as one of the arguments. If that Object is a String, it will display a String. If that Object is a Container containing buttons, input fields and other widgets, it will display that.
To quote http://download.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html
Parameters: The parameters to these methods follow consistent patterns: parentComponent
Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be null, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).
message
A descriptive message to be placed in the dialog box. In the most common usage, message is just a String or String constant. However, the type of this parameter is actually Object. Its interpretation depends on its type:
Object[] An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is recursive -- each object in the array is interpreted according to its type.
Component The Component is displayed in the dialog.
Icon The Icon is wrapped in a JLabel and displayed in the dialog.
others The object is converted to a String by calling its toString method. The result is wrapped in a JLabel and displayed.
You have two options here.
Either go and use JOptionPane methods to show a dialog as detailed by Paul Tomblin
or build your own dialog.
The second option is a necessity if you are after a fine grained control of a dialog, e.g. if you need different names (this can be done also by use of JOptionPane.showOptionDialog) or location of buttons on the dialog, or if you need a not modal dialog.
Simple Example:
import java.awt.Color;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class DialogsTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JPanel p = new JPanel();
JPanel contentPane = new JPanel();
contentPane.add(p);
JFrame f = new JFrame();
f.setContentPane(contentPane);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
/*
* NOTE: It is not recomended to share the same instance of a component
* by different parents. Thought it is fine here since the first
* dialog will release it before the second will get it.
* But in a situation when we wouldn't make the 'dialog' modal and
* we would show it after the 'option pane dialog' it would be empty.
*/
JPanel message = new JPanel();
message.add(new JLabel("Label:"));
message.add(new JTextField("ABCD"));
message.setBackground(Color.GREEN);
JOptionPane.showConfirmDialog(f, message, "Default made dialog", JOptionPane.YES_NO_OPTION);
Object[] options = new String[]{"a", "b", "c"};
JOptionPane.showOptionDialog(f, message, "", JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
JDialog dialog = new JDialog(f, "Custom made dialog");
dialog.setModal(true);
dialog.setContentPane(message);
dialog.pack();
dialog.setLocationRelativeTo(f);
dialog.setVisible(true);
}
});
}
}
BTW you have a very nice (maybe too rich) example in the Java tutorial you read.
精彩评论