Search code examples
javaswingjframejtextfieldspringlayout

How to get the input from a JFrame form?


I am trying to get the input that I submit in a JFrame form and store it in an arraylist. I am not sure how to get what input i place in the text field when my ActionEvent occurs. Can anyone tell me how to do it?

my form currently looks like This

Here is my code:

public class SpringDemo {
    private static void createAndShowGUI() {
        final String[] labels = {"Bill: ", "Last Top Up Date: ", "Same Network? "};
        int labelsLength = labels.length;

        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        for (int i = 0; i < labelsLength; i++) {
            JLabel l = new JLabel(labels[i], JLabel.TRAILING);
            p.add(l);
            JTextField textField = new JTextField(10);
            l.setLabelFor(textField);
            p.add(textField);
        }
        JButton button = new JButton("Submit");
        p.add(new JLabel());
        p.add(button);

        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
                                    labelsLength + 1, 2, //rows, cols
                                    7, 7,        //initX, initY
                                    7, 7);       //xPad, yPad

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                //Execute when button is pressed
                System.out.println("Test");
            }
        });  
        //Create and set up the window.
        JFrame frame = new JFrame("SpringForm");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        p.setOpaque(true);  //content panes must be opaque
        frame.setContentPane(p);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Solution

  • To do this your createAndShowGUI method should be like this:

    private static void createAndShowGUI() {
        final String[] labels = {"Bill: ", "Last Top Up Date: ", "Same Network? "};
        int labelsLength = labels.length;
        final JTextField[] textField = new JTextField[labels.length];
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        for (int i = 0; i < labelsLength; i++) {
            JLabel l = new JLabel(labels[i], JLabel.TRAILING);
            p.add(l);
            textField[i] = new JTextField(10);
            l.setLabelFor(textField[i]);
            p.add(textField[i]);
        }
        JButton button = new JButton("Submit");
        p.add(new JLabel());
        p.add(button);
    
        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
                                        labelsLength + 1, 2, //rows, cols
                                        7, 7,        //initX, initY
                                        7, 7);       //xPad, yPad
    
        button.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent e)
            {
                for (int i = 0 ; i < labels.length ; i++)
                {
                    System.out.println(labels[i]+"->"+textField[i].getText());
                }
            }
        });  
        //Create and set up the window.
        JFrame frame = new JFrame("SpringForm");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        //Set up the content pane.
        p.setOpaque(true);  //content panes must be opaque
        frame.setContentPane(p);
    
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }