import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class OptionPanel extends JPanel implements ActionListener { JLabel titleOptional; JLabel messageTypeOptional; JLabel optionTypeOptional; JLabel iconOptional; JLabel optionObjectOptional; ButtonGroup dialogGroup; // Panel 1 JRadioButton showMess; JRadioButton showConf; JRadioButton showInput; JRadioButton showOption; JCheckBox stringCheck; // Panel 2 JCheckBox iconCheck; JCheckBox componentCheck; JCheckBox otherCheck; JTextField titleText; // Panel 3 JComboBox messageTypeCombo; // Panel 4 JList optionTypeList; // Panel 5 JRadioButton showAnIcon; JCheckBox optionCheck; // Panel 6 JButton goButton; public OptionPanel() { super(new GridLayout(2,3)); // 2 rows 3 cols int i = 0; add(initDialogType()); add(initMessage()); add(initTitle()); add(initMessageType()); add(initOptionType()); add(initOptionObject()); showMess.doClick(); } private JPanel initDialogType() { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), // border type "1 - Option Dialog Type")); // border title Box n = Box.createVerticalBox(); dialogGroup = new ButtonGroup(); showMess = new JRadioButton("showMessageDialog", true); showMess.addActionListener(this); showMess.setActionCommand("showMessageDialog"); dialogGroup.add(showMess); n.add(showMess); showConf = new JRadioButton("showConfirmDialog", false); showConf.addActionListener(this); showConf.setActionCommand("showConfirmDialog"); dialogGroup.add(showConf); n.add(showConf); showInput = new JRadioButton("showInputDialog", false); showInput.addActionListener(this); showInput.setActionCommand("showInputDialog"); dialogGroup.add(showInput); n.add(showInput); showOption = new JRadioButton("showOptionDialog", false); showOption.addActionListener(this); showOption.setActionCommand("showOptionDialog"); dialogGroup.add(showOption); n.add(showOption); p.add(n, "West"); return p; } private JPanel initMessage() { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "2 - The message")); Box n = Box.createVerticalBox(); stringCheck = new JCheckBox("The string 'Hello!'"); stringCheck.setToolTipText("You are free to use an " + "Object, a String is an " + "Object"); n.add(stringCheck); iconCheck = new JCheckBox("An Icon"); iconCheck.setToolTipText("... use an Icon ..."); n.add(iconCheck); componentCheck = new JCheckBox("A Component"); componentCheck.setToolTipText("... use a Component ..."); n.add(componentCheck); otherCheck = new JCheckBox("An Object's toString()"); otherCheck.setToolTipText("... or other objects that " + "will use their toString " + "method"); n.add(otherCheck); JLabel s = new JLabel("Tip"); s.setToolTipText("Multiple types are allowed and are " + "treated as an Object[]"); n.add(s); p.add(n, "West"); stringCheck.setSelected(true); return p; } private JPanel initTitle() { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "3 - Title")); Box n = Box.createVerticalBox(); n.add(new JLabel("The title is optional to most")); n.add(new JLabel("dialog types but is required")); n.add(new JLabel("using showOptionMessage,")); n.add(new JLabel("though it is possible to use")); n.add(new JLabel("null ( = empty ).")); n.add(new JLabel(" ")); /* A text field for user defined titles */ titleOptional = new JLabel(" "); n.add(titleOptional); titleText = new JTextField("Title"); n.add(titleText); p.add(n, "North"); // try west and see the difference return p; } private JPanel initMessageType() { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "4 - The Message Type")); Box n = Box.createVerticalBox(); n.add(new JLabel(" ")); messageTypeOptional = new JLabel(" "); n.add(messageTypeOptional); messageTypeCombo = new JComboBox(); messageTypeCombo.addItem("ERROR_MESSAGE"); messageTypeCombo.addItem("INFORMATION_MESSAGE"); messageTypeCombo.addItem("WARNING_MESSAGE"); messageTypeCombo.addItem("QUESTION_MESSAGE"); messageTypeCombo.addItem("PLAIN_MESSAGE"); messageTypeCombo.addItem("none"); messageTypeCombo.setSelectedIndex(5); n.add(messageTypeCombo); p.add(n, "North"); // try west and see the difference return p; } private JPanel initOptionType() { JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "5 - The Option Type")); Box n = Box.createVerticalBox(); n.add(new JLabel(" ")); optionTypeOptional = new JLabel(" "); n.add(optionTypeOptional); String[] options = {"DEFAULT_OPTION", "YES_NO_OPTION", "YES_NO_CANCEL_OPTION", "OK_CANCEL_OPTION", "none"}; optionTypeList = new JList(options); optionTypeList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION); optionTypeList.setVisibleRowCount(2); n.add(new JScrollPane(optionTypeList)); // The icon choice n.add(new JLabel(" ")); n.add(new JLabel("--------------------------------")); n.add(new JLabel("The icon choice")); iconOptional = new JLabel(" "); n.add(iconOptional); ButtonGroup grp = new ButtonGroup(); showAnIcon = new JRadioButton("User defined icon"); grp.add(showAnIcon); n.add(showAnIcon); JRadioButton showNoIcon = new JRadioButton("Default message icons"); grp.add(showNoIcon); n.add(showNoIcon); showNoIcon.setSelected(true); p.add(n, "North"); return p; } private JPanel initOptionObject() { JPanel p = new JPanel(new BorderLayout()); JPanel pn = new JPanel(new BorderLayout()); pn.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "6 - The Option List")); Box n = Box.createVerticalBox(); n.add(new JLabel(" ")); optionObjectOptional = new JLabel(" "); n.add(optionObjectOptional); optionCheck = new JCheckBox("The Option Object[]"); optionCheck.setToolTipText("Select from an array of " + "options"); n.add(optionCheck); n.add(new JLabel(" Initial value is set to 1")); n.add(new JLabel(" ")); n.add(new JLabel(" ")); pn.add(n, "West"); p.add(pn, "North"); JPanel ps = new JPanel(); goButton = new JButton("Show dialog"); goButton.setBackground(Color.white); goButton.addActionListener(this); ps.add(goButton); p.add(ps, "South"); return p; } public void actionPerformed(ActionEvent evt) { Object cmdSource = evt.getSource(); if (cmdSource == goButton) { String dialogType = getDialogType(); if (dialogType.equals("showMessageDialog")) { String title = getTitle(); if (title == null) title = "Message"; // The default title int messageType = getMessageType(); if (messageType == -1) // none messageType = JOptionPane.INFORMATION_MESSAGE; JOptionPane.showMessageDialog( this, getMessage(), title, messageType, getIcon() ); } else if (dialogType.equals("showConfirmDialog")) { String title = getTitle(); if (title == null) title = "Select an Option"; // Default title int optionType = getOptionType(); if (optionType == -1) // none optionType = JOptionPane.YES_NO_CANCEL_OPTION; int messageType = getMessageType(); if (messageType == -1) // none messageType = JOptionPane.QUESTION_MESSAGE; JOptionPane.showConfirmDialog( this, getMessage(), title, optionType, messageType, getIcon() ); } else if (dialogType.equals("showInputDialog")) { String title = getTitle(); if (title == null) title = "Input"; // Default title int messageType = getMessageType(); if (messageType == -1) // none messageType = JOptionPane.QUESTION_MESSAGE; Object[] optionObject = null; Object selectedObject = null; if (optionCheck.isSelected()) { optionObject = new Object[3]; optionObject[0] = "Hello"; optionObject[1] = new ImageIcon("chicken.gif"); optionObject[2] = new JLabel("HelloLabel"); selectedObject = optionObject[0]; } JOptionPane.showInputDialog( this, getMessage(), title, messageType, getIcon(), optionObject, selectedObject ); } else if (dialogType.equals("showOptionDialog")) { Object[] optionObject = new Object[3]; optionObject[0] = "Hello"; optionObject[1] = new ImageIcon("chicken.gif"); optionObject[2] = new Double(Math.PI); Object selectedObject = optionObject[0]; JOptionPane.showOptionDialog( this, getMessage(), getTitle(), getOptionType(), getMessageType(), getIcon(), optionObject, selectedObject ); } } else if (cmdSource == showMess) { System.out.println("showMess"); titleOptional.setText("Optional"); messageTypeOptional.setText("Optional"); optionTypeOptional.setText("Not applicable"); optionTypeList.setSelectedIndex(4); iconOptional.setText("Optional"); optionObjectOptional.setText("Not applicable"); optionCheck.setSelected(false); } else if (cmdSource == showConf) { System.out.println("showConf"); titleOptional.setText("Optional"); messageTypeOptional.setText("Optional"); optionTypeOptional.setText("Optional"); iconOptional.setText("Optional"); optionObjectOptional.setText("Not applicable"); optionCheck.setSelected(false); } else if (cmdSource == showInput) { System.out.println("showInput"); titleOptional.setText("Optional"); messageTypeOptional.setText("Optional"); optionTypeOptional.setText("Not applicable"); optionTypeList.setSelectedIndex(4); iconOptional.setText("Optional"); optionObjectOptional.setText("Optional"); } else if (cmdSource == showOption) { System.out.println("showOption"); titleOptional.setText("Required"); messageTypeOptional.setText("Required"); optionTypeOptional.setText("Required"); iconOptional.setText("Required"); optionObjectOptional.setText("Required"); optionCheck.setSelected(true); } } private String getDialogType() { String dialType = dialogGroup.getSelection().getActionCommand(); System.out.println(dialType); return dialType; } private Object getMessage() { Object[] messages = new Object[4]; int msgNumber = 0; if (stringCheck.isSelected()) { messages[msgNumber++] = new String("Hello"); } if (iconCheck.isSelected()) { messages[msgNumber++] = new ImageIcon("chicken.gif"); } if (componentCheck.isSelected()) { JLabel label = new JLabel("HelloLabel"); label.setForeground(Color.red); messages[msgNumber++] = label; } if (otherCheck.isSelected()) { Vector vec = new Vector(); vec.addElement("Hello"); vec.addElement(new Double(Math.PI)); vec.addElement(new Point(3, 55)); messages[msgNumber++] = vec; } if (msgNumber == 0) { // this is not allowed JOptionPane.showMessageDialog(this, "At least one option must be used.\n" + "'Hello' is used this time."); return "Hello"; } else if (msgNumber == 1) { // avoid returning a one-item array return messages[0]; } else { Object[] mess = new Object[msgNumber]; for (int i = 0; i < msgNumber; i++) { mess[i] = messages[i]; } return mess; } } private String getTitle() { String title = titleText.getText(); if (title.equals("Title") || title.equals("")) { return null; } else { return title; } } private int getMessageType() { int type = messageTypeCombo.getSelectedIndex(); if (type == 0) { return JOptionPane.ERROR_MESSAGE; } else if (type == 1) { return JOptionPane.INFORMATION_MESSAGE; } else if (type == 2) { return JOptionPane.WARNING_MESSAGE; } else if (type == 3) { return JOptionPane.QUESTION_MESSAGE; } else if (type == 4) { return JOptionPane.PLAIN_MESSAGE; } else { // none return -1; } } private int getOptionType() { int type = optionTypeList.getSelectedIndex(); if (type == 0) { return JOptionPane.DEFAULT_OPTION; } else if (type == 1) { return JOptionPane.YES_NO_OPTION; } else if (type == 2) { return JOptionPane.YES_NO_CANCEL_OPTION; } else if (type == 3) { return JOptionPane.OK_CANCEL_OPTION; } else { // none return -1; } } private Icon getIcon() { if (showAnIcon.isSelected()) { return new ImageIcon("chicken.gif"); } else { return null; } } }