Sunday, June 14, 2015

JOptionPane with Multiple Inputs

Assume that you want to provide a JOptionPane, Input Message Dialog with multiple input fields. There are several ways that you can do this. Among them this is one simple solution that you can try out.

Following is the Input Dialog that we are going to develop.


Following is the main method that you have to use in order to develop the JOptionPane with multiple inputs.
private void showMultipleInputMessageDialog() {

        textArea.setText("");
        JTextField textField1 = new JTextField();
        final JTextField textField2 = new JTextField();
        final JCheckBox checkBox = new JCheckBox();
        textField2.setEnabled(false);

        checkBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (e.getSource() == checkBox) {
                    if (checkBox.isSelected()) {
                        textField2.setEnabled(true);
                    } else {
                        textField2.setEnabled(false);
                    }
                }
            }
        });

        Object[] inputFields = {"Enter Text 01", textField1,
                "Enable TextField 02", checkBox,
                "Enter Text 02", textField2};

        int option = JOptionPane.showConfirmDialog(this, inputFields, "Multiple Inputs", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

        if (option == JOptionPane.OK_OPTION) {
            String text = textField1.getText() + "\n" + (checkBox.isSelected() ? "Checked" : "Unchecked") + "\n" + textField2.getText() + "\n";
            textArea.setText(text);
        }
    }

Following is the complete JFrame with the demo application.

package com.dummy.learning;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Kinath.
 */
public class ApplicationFrame extends JFrame implements ActionListener {

    private static ApplicationFrame instance;

    JPanel jPanel = new JPanel();
    JButton jButton = new JButton("Click");
    JTextArea textArea = new JTextArea(5, 100);
    GridBagLayout gridBagLayout1 = new GridBagLayout();
    GridBagLayout gridBagLayout2 = new GridBagLayout();

    public static ApplicationFrame getInstance() {
        if (instance == null) {
            instance = new ApplicationFrame();
        }
        return instance;
    }

    public ApplicationFrame() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(new Dimension(300, 250));
        this.setTitle("JOptionPane Demo");
        this.setResizable(false);
        setGui();
    }

    private void setGui() {
        this.setLayout(gridBagLayout1);
        this.getContentPane().add(jPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        jPanel.setLayout(gridBagLayout2);
        jPanel.setBorder(BorderFactory.createEtchedBorder());
        jButton.setMinimumSize(new Dimension(100, 25));
        jButton.setMaximumSize(new Dimension(100, 25));
        jButton.setPreferredSize(new Dimension(100, 25));
        jButton.addActionListener(this);
        jPanel.add(jButton, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        jPanel.add(textArea, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton) {
            showMultipleInputMessageDialog();
        }
    }

    private void showMultipleInputMessageDialog() {

        textArea.setText("");
        JTextField textField1 = new JTextField();
        final JTextField textField2 = new JTextField();
        final JCheckBox checkBox = new JCheckBox();
        textField2.setEnabled(false);

        checkBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (e.getSource() == checkBox) {
                    if (checkBox.isSelected()) {
                        textField2.setEnabled(true);
                    } else {
                        textField2.setEnabled(false);
                    }
                }
            }
        });

        Object[] inputFields = {"Enter Text 01", textField1,
                "Enable TextField 02", checkBox,
                "Enter Text 02", textField2};

        int option = JOptionPane.showConfirmDialog(this, inputFields, "Multiple Inputs", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

        if (option == JOptionPane.OK_OPTION) {
            String text = textField1.getText() + "\n" + (checkBox.isSelected() ? "Checked" : "Unchecked") + "\n" + textField2.getText() + "\n";
            textArea.setText(text);
        }
    }
}

Hope that helps.
Thank You. :-)

1 comment:

  1. The article is too good and it will be helpful for java developers.Thanks for sharing such useful information.
    Java training in Chennai

    ReplyDelete