Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Keep the <code>JTextField</code>s in a List, and refer to them in your action listener.</p> <pre><code>import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SpringLayout; public class SpringDemo { private static void createAndShowGUI() { final String[] labels = { "Bill: ", "Last Top Up Date: ", "Same Network? " }; int labelsLength = labels.length; final List&lt;JTextField&gt; textFields = new ArrayList&lt;JTextField&gt;(); // Create and populate the panel. JPanel p = new JPanel(new SpringLayout()); for (int i = 0; i &lt; labelsLength; i++) { JLabel l = new JLabel(labels[i], JLabel.TRAILING); p.add(l); JTextField textField = new JTextField(10); textFields.add(textField); 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"); for (JTextField jTextField : textFields) { String s = jTextField.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); } 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(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload