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.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
nunos
  • 20,479
  • 50
  • 119
  • 154

2 Answers2

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();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Sorry about the initial confusion between answer and example. – trashgod Jun 09 '10 at 03:20
  • How can I retrieve the input? – nunos Jun 09 '10 at 10:37
  • I've elaborated above and added a link to the tutorial. – trashgod Jun 09 '10 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: http://tips4java.wordpress.com/2010/03/14/dialog-focus/ – camickr Jun 09 '10 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 09 '10 at 15:01
  • I keep forgetting I can edit postings and it is easier to edit the posting to add a link. I'll try an remember the next time :) – camickr Jun 09 '10 at 15:18
  • + You will soon earn the swing specialist badge ;-) – stacker Jun 09 '10 at 18:08
  • Exactly the same example I had given , though mine is hard, yours is good :-) – nIcE cOw May 14 '12 at 17:05
  • Wow, this is a fantastic example of a quick popup box. Thanks a lot! – Singular1ty Mar 09 '13 at 01:19
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

milchreis
  • 79
  • 7