Home > Java > javaTutorial > How to Create a Popup Form with Multiple JTextFields and JLabels Using JOptionPane?

How to Create a Popup Form with Multiple JTextFields and JLabels Using JOptionPane?

Susan Sarandon
Release: 2024-11-08 15:07:02
Original
837 people have browsed it

How to Create a Popup Form with Multiple JTextFields and JLabels Using JOptionPane?

Using Multiple JTextFields and JLabels to Display a Popup Form

Problem:

A user's requirement is to present a popup form containing at least two JTextFields and JLabels upon button click, but the use of JOptionPane.showInputDialog() is not an acceptable solution.

Answer:

Consider utilizing JOptionPane methods like showInputDialog() or showMessageDialog() despite the number of components.

Additional Considerations:

  • The selection of JOptionPane is more dependent on the modality required than on the number of elements displayed.
  • By employing the approach described in "Dialog Focus," you can direct focus to a specific component.

Sample Code:

The following code snippet demonstrates the implementation of a popup form using a JPanel, JComboBox, and JTextFields with JOptionPane.

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

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();
            }
        });
    }
}
Copy after login

The above is the detailed content of How to Create a Popup Form with Multiple JTextFields and JLabels Using JOptionPane?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template