Dialog boxes are tools that provide interactive modes for human-computer dialogue processes. The application either provides information to the user through a dialog box, or obtains information from the user. A dialog box is a temporary window where you can place controls for getting user input. In Swing, there are two dialog classes, which are the JDialog class and the JOptionPane class. The JDialog class provides the construction and management of common dialog boxes; the JOptionPane class provides many easy-to-use options for some common dialog boxes, such as the simple "yes-no" dialog box, etc.
JDialog class
The JDialog class serves as the base class for the dialog box. Dialog boxes are different from general windows. Dialog boxes rely on other windows. When the window it depends on disappears or is minimized, the dialog box will also disappear; when the window is restored, the dialog box will automatically resume.
Dialog boxes are divided into mandatory and non-mandatory. The mandatory dialog box cannot interrupt the dialogue process until the dialog box is finished, and the program will not respond to events outside the dialog box. A non-mandatory dialog box can interrupt the dialogue process and respond to events outside the dialog box. Forced type is also called a mode dialog box, and non-forced dialog box is also called a non-mode dialog box.
JDialog objects are also a container, so you can also assign a layout manager to the JDialog dialog box. The default layout of the dialog box is the BoardLayout layout. However, components cannot be added directly to the dialog box. The dialog box also contains a content panel, and the components should be added to the content panel of the JDialog object. Since a dialog depends on a window, to create a dialog, you must first create a window.
There are 3 commonly used constructors for JDialog class:
There are several other common methods of the JDialog class:
[Example] The applet declares a user window class and a dialog box class. The user window has two buttons and two text boxes. When a button is clicked, the corresponding dialog box is activated. Enter the corresponding information in the dialog box and press the OK button in the dialog box. Determine the monitoring method of the button, transfer the information entered in the dialog box to the user window, and display the selection information in the corresponding text box of the user window.
import java.applet.*import javax.swing.*;import java.awt.*;import java.awt.event.*;class MyWindow extends JFrame implements ActionListener{ p rivate JButton button1,button2; private static int flg=0; private static JTextField text1,text2; Mywindow(String s){ super(s); Container con = this.getContentPane(); con.setLayout(new GridLayout(2,2)); this.se tSize(200,100); this setLocation(100,100 ); button1 = new JButton("Select fruit"); button2 = new JButton("Select food"); button1.addActionListener(this); button2.addActionListener(this); text1 = new JTe xtField(20); text2 = new JTextField (20); con.add(button1); con.add(button2); con.add(text1); con.add(text2); this.setVisible(true); this.pack(); } public static void retur nName (String s){ if(flg ==1) text1.setText("The selected fruit is: "+s); else if(flg == 2) text2.setText("The selected food is: "+s); } public void actionPerformed(ActionEvent e){ MyDialog dialog; if(e.getSource()==button1){ dialog = new MyDialog(this,"fruit"); dialog.set Visible(true); flg =1; } else if (e.getSource()==button2){ dialog =new MyDialog(this,"food"); dialog.setVisible(true); flg=2; } }}class MyDialog extends JDialog im tributes ActionListener{ JLabel title; JTextField text; JButton done; Mydialog(JFrame F,String s){ super(F,s,true);//Modal Container con = this.getContentPane(); title = new JLabel("Input"+s+"name"); text = new JTextField(10); text.setEditable(true); con.setLayout(new FlowLayout()); con.setSize(200,100); setModal(false); done = new JButton(" OK"); done.addActionListener( this); con.setVisible(true); this.pack(); } public void actionPerformed(ActionEvent e){ MyWindow.returnName(text.getText()); setVisible(f also); dispose(); }}public class Example6_6 extends Applet{ MyWindow window; MyDialog dialog; public void init(){ window = new MyWindow("Window with dialog box"); }}The above example creates a mandatory dialog box. Changing it to a non-mandatory dialog box allows the user to pause during the conversation and interact with other parts of the program. In this way, you can see the effect of some dialogues in the interface.
Change the above example to a non-mandatory dialog box with just a small amount of changes. First, change the code "super(F,s,true);" in the dialog box construction method to "super(F,s,false);".
The second change: It turns out that the method returnName() is called when responding to the confirm button event, and the string obtained by the dialog box is returned to the program. Now when the text box input selection string ends, the method should be called immediately. To do this, you need to monitor the input events in the text box and register the monitor for the text:
public void actionPerformed(ActionEvent e){ if(e.getSource()==text){ MyWindow.returnName(text.getText()); } else if(e.getSource()==don e){ MyWindow.returnName(text .getText()); setVisible(false); dispose();//Clear resource}}JOptionPane class
It is often encountered very simple dialogue situations. In order to simplify the programming of common dialog boxes, the JOptionPane class defines four simple dialog boxes types, see Table 11-4. The JOptionPane class provides a set of static methods to allow users to select a certain type of dialog box. The following code is the check dialog box:
int result = JOptionPane.showConfirmDialog(parent, "Do you really want to exit?", "Exit Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
The text "Confirm" in the middle part of the method name is the type of creating a dialog box, and the text Confirm indicates that the confirmation dialog box is selected. Change the text Confirm to one of the other three types and becomes the corresponding type dialog box. The meaning of the four parameters of the above code is: the first parameter specifies the parent window of this dialog box; the second parameter is the text displayed on the dialog box; the third parameter is the title of the dialog box; the last parameter specifies that the dialog box has three The buttons are "Yes (Y), "No (N), and "Undo". The return result of the method is the result of the user responding to this dialog box, see the possible answers given in Table 11-5.
The input dialog box requests the user to enter selection information in the form of a list or text box. The user can select options from the list or enter information from the text box. Here is a schematic code for a input dialog box that selects a running project from the list:
String result = (String)JOptionPane.showInputDialog(parent, "Please select a sport", "This is the sport selection dialog", JOptionPane.QUESTION_MESSAGE,null, new Object[]{"play football"," Play basketball ”, “running”, “jump rope”}, “running”); The fourth parameter is the information type, the fifth parameter has no special effect here, and is always null; the sixth parameter defines an array of strings to choose, and the seventh parameter is the default value of the choice. The dialog box also includes the OK and Undo buttons.
JOptionPane dialog box type
Results returned by the JOptionPane dialog box
Information type options for the JOptionPane dialog box
Sometimes, the program simply outputs some information and does not require the user to have feedback. Such a dialog box can be created with the following form of code:
JOptionPane.showMessageDialog(parent, "This is a Java program", "I am the output information dialog box", JOptionPane.PLAIN_MESSAGE);
The meaning of the first three parameters in the above code is the same as described above, and the final parameter is that the information type is specified to not include any icons.