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:
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(); } }); } }
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!