Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4626924
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:29:30+00:00 2026-05-22T03:29:30+00:00

Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which

  • 0

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:

ArrayList names,
ArrayList values,
int cols

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.

public class SampleFiveA extends JPanel {


ArrayList<String> names = new ArrayList<String>(); //the text for the labels
ArrayList<String> values = new ArrayList<String>(); // 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);


};
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:29:31+00:00Added an answer on May 22, 2026 at 3:29 am

    Here’s a start for you.

    It adds a FocusListener to the text fields and makes sure that the content of the ArrayList is updated with the current value when the text field looses focus.

    import java.awt.GridLayout;
    import java.awt.event.*;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    public class Main extends JPanel {
    
        ArrayList<String> names = new ArrayList<String>(); // the text for the
                                                           // labels
        ArrayList<String> values = new ArrayList<String>(); // the initial contents
                                                            // of the text fields
        int col; // the number of columns used to set the width of each textfield
    
        public Main() {
            JPanel p = new JPanel();
            p.setLayout(new GridLayout(2, 2));
    
            names = new ArrayList<String>();
            values = new ArrayList<String>();
    
    
            JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
            p.add(lab1);
            JTextField txt1 = new JTextField("User Name", JTextField.RIGHT);
            p.add(txt1);
    
            names.add(lab1.getText());
            values.add(txt1.getText());
    
            JLabel lab2 = new JLabel("Password ", JLabel.LEFT);
            p.add(lab2);
            JPasswordField txt2 = new JPasswordField("*****", JPasswordField.RIGHT);
            p.add(txt2);
            names.add(lab2.getText());
            values.add(txt2.getText());
    
    
            // names.add(lab1,lab2);// Not right but I want to put the label text to
            // this arrayList
            // values.add(txt1,txt2);//
            txt1.addFocusListener(new ArrayListFocusListener(txt1, values, 0));
            txt2.addFocusListener(new ArrayListFocusListener(txt2, values, 1));
    
            add(p);
    
    
            // Start a thread to print the content of the list for 10 seconds
            new Thread() {
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            sleep(1000);
                        } catch (InterruptedException e) {
                        }
                        System.out.println(values);
                    }
                }
            }.start();
    
    
        };
    
    
    
        public static void main(String[] args) {
    
            JFrame frame = new JFrame();
            frame.getContentPane().add(new Main());
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(200, 200);
            frame.setVisible(true);
    
        };
    
        class ArrayListFocusListener implements FocusListener {
            JTextField textField;
            ArrayList<String> backingList;
            int myIndex;
    
            public ArrayListFocusListener(JTextField textField,
                    ArrayList<String> backingList, int myIndex) {
                this.textField = textField;
                this.backingList = backingList;
                this.myIndex = myIndex;
            }
    
            public void focusGained(FocusEvent e) {
            }
    
            @Override
            public void focusLost(FocusEvent e) {
                backingList.set(myIndex, textField.getText());
            }
    
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Person { string name; public Person(string name) { this.name = name; } public
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class
class C { T a; public: C(T a): a(a) {;} }; Is it legal?
class Score { var $score; var $name; var $dept; var $date; function Score($score, $name,
class MyBase { protected object PropertyOfBase { get; set; } } class MyType :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.