16

When the user clicks a button, I want to show a popup form that should have at least two JTextFields and two JLabels, so using JOptionPane.showInputDialog is not a possibility.

2 Answers 2

41

You should at least consider one of the JOptionPane methods such as showInputDialog() or showMessageDialog().

Addendum: The choice to use JOptionPane hinges more on the suitability of modality, rather than on the number of components shown. See also How to Make Dialogs.

Addendum: As noted in a comment by @camickr, you can set the focus to a particular component using the approach discussed in Dialog Focus, cited here.

image

package gui;

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.*;

/** @see https://stackoverflow.com/a/3002830/230513 */
class JOptionPaneTest {

    private static void display() {
        String[] items = {"One", "Two", "Three", "Four", "Five"};
        JComboBox<String> combo = new JComboBox<>(items);
        JTextField field1 = new JTextField("1234.56");
        JTextField field2 = new JTextField("9876.54");
        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(combo);
        panel.add(new JLabel("Field 1:"));
        panel.add(field1);
        panel.add(new JLabel("Field 2:"));
        panel.add(field2);
        int result = JOptionPane.showConfirmDialog(null, panel, "Test",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            System.out.println(combo.getSelectedItem()
                + " " + field1.getText()
                + " " + field2.getText());
        } else {
            System.out.println("Cancelled");
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }
}
9
  • Sorry about the initial confusion between answer and example.
    – trashgod
    Jun 9, 2010 at 3:20
  • How can I retrieve the input?
    – nunos
    Jun 9, 2010 at 10:37
  • I've elaborated above and added a link to the tutorial.
    – trashgod
    Jun 9, 2010 at 13:32
  • 3
    +1, for showing how an option pane can be used for this and for including a link to the tutorial. There is one problem with using a panel this way. Focus will appear on the button, not the text field which may be a problem. For a solution to this check out the "Dialog Focus" link: tips4java.wordpress.com/2010/03/14/dialog-focus
    – camickr
    Jun 9, 2010 at 14:10
  • 1
    @camickr: Thank you for the link to that very helpful article, which I've cited above. Your considerable experience often anticipates the next likely question. I welcome additions and corrections; please don't hesitate to edit my answers as the opportunity arises.
    – trashgod
    Jun 9, 2010 at 15:01
1

With the small UiBooster library you can build a form dialog in a few lines of code.

FilledForm form = new UiBooster()
        .createForm("Personal informations")
        .addText("Whats your first name?")
        .addTextArea("Tell me something about you")
        .addSelection(
                "Whats your favorite movie?",
                Arrays.asList("Pulp Fiction", "Bambi", "The Godfather", "Hangover"))
        .show();

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.