title: Using ArrayList in a JPanel
body: <p>Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which has a label on the left. All textfields will be of the same width and positioned against the right border of the panel. SampleFiveA  has only one constructor that accepts the following three parameters:</p>
  
  <p>ArrayList names,
  ArrayList values,
  int cols </p>
  
  <p>I so far created the sample username password screen in GUI but now I have a problem implementing an ArrayList in JPanel one for User Name and the other for password. Kind of stuck there for hours now cant find a proper example to do it. Below is the code I commented what I need to be done using ArrayList.</p>
  
  <pre><code>public class SampleFiveA extends JPanel {
  
  
  ArrayList&lt;String&gt; names = new ArrayList&lt;String&gt;(); //the text for the labels
  ArrayList&lt;String&gt; values = new ArrayList&lt;String&gt;(); // the initial contents of the text fields
  int col ; //the number of columns used to set the width of each textfield
  
  
  public SampleFiveA()
  {
          JPanel p = new JPanel();
          p.setLayout(new GridLayout(2,2)); 
  
          JLabel lab1 = new JLabel("User Name", JLabel.LEFT);         
          p.add(lab1 = new JLabel("User Name"));
          JTextField txt1 = new JTextField("User Name", JTextField.RIGHT);
          p.add(txt1= new JTextField());
  
  
  
          JLabel lab2 = new JLabel("Password ", JLabel.LEFT);            
          p.add(lab2 = new JLabel("Password"));
          JPasswordField txt2 = new JPasswordField("*****",JPasswordField.RIGHT );
          p.add(txt2 = new JPasswordField());
  
  
  
        //  names.add(lab1,lab2);// Not right but I want to put the label text to this arrayList
        //  values.add(txt1,txt2);// 
  
  
          add(p);
  };
  
  
  
  public static void main(String[] args) 
  {
  
  
  JFrame frame = new JFrame();
  frame.getContentPane().add(new SampleFiveA());
  
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(200,200);
  frame.setVisible(true);
  
  
  };
  };
  </code></pre>