import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DialogExample implements ActionListener {
JDialog dialog;
JTextField textField;
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if(ac.equals("CANCEL"))
System.exit(0);
if(ac.equals("CLEAR"))
textField.setText("");
if(ac.equals("OK")) {
String text = textField.getText();
if(!text.equals("")) {
dialog.dispose();
JOptionPane.showMessageDialog(null, "start your main app");
// For now we need this line.
System.exit(0);
}
}
}
private JPanel getContent() {
textField = new JTextField(16);
String[] ids = { "Cancel", "Clear", "OK" };
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridwidth = 3;
panel.add(textField, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1;
for(int j = 0; j < ids.length; j++) {
JButton button = new JButton(ids[j]);
button.setActionCommand(ids[j].toUpperCase());
button.addActionListener(this);
panel.add(button, gbc);
}
return panel;
}
private void launchDialog() {
dialog = new JDialog(new Frame(), "dialog", false);
dialog.addWindowListener(closer);
dialog.getContentPane().add(getContent());
dialog.setSize(300,200);
dialog.setLocation(200,200);
dialog.setVisible(true);
}
public static void main(String[] args) {
DialogExample example = new DialogExample();
example.launchDialog();
}
private WindowListener closer = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
}