fubarable

Untitled

Sep 23rd, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.awt.GridBagConstraints;
  2. import java.awt.GridBagLayout;
  3. import java.awt.Insets;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import javax.swing.JLabel;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JPanel;
  9. import javax.swing.JTextField;
  10. import javax.swing.SwingUtilities;
  11.  
  12. public class Test {
  13.     public static void main(String[] args) {
  14.         SwingUtilities.invokeLater(() -> {
  15.             String[] labels = { "Last Name", "First Name", "Number", "Age", "Skill Level" };
  16.             Map<String, JTextField> labelFieldMap = new HashMap<>();
  17.  
  18.             JPanel inputPanel = new JPanel(new GridBagLayout());
  19.             GridBagConstraints gbc = new GridBagConstraints();
  20.             gbc.gridx = 0;
  21.             gbc.gridy = 0;
  22.             int ins = 3;
  23.             gbc.insets = new Insets(ins, ins, ins, ins);
  24.             gbc.fill = GridBagConstraints.HORIZONTAL;
  25.  
  26.             for (String labelText : labels) {
  27.                 gbc.gridx = 0;
  28.                 gbc.gridy = GridBagConstraints.RELATIVE;
  29.                 inputPanel.add(new JLabel(labelText), gbc);
  30.                 JTextField textField = new JTextField(10);
  31.                 labelFieldMap.put(labelText, textField);
  32.                 gbc.gridx = 1;
  33.                 inputPanel.add(textField, gbc);
  34.             }
  35.             int selection = JOptionPane.showConfirmDialog(null, inputPanel, "Enter Employee Information",
  36.                     JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  37.             if (selection == JOptionPane.OK_OPTION) {
  38.                 for (String labelText : labels) {
  39.                     System.out.printf("%s: %s%n", labelText,
  40.                             labelFieldMap.get(labelText).getText());
  41.                 }
  42.             }
  43.         });
  44.     }
  45. }
Add Comment
Please, Sign In to add comment