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

  • SEARCH
  • Home
  • 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 7905251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:24:15+00:00 2026-06-03T10:24:15+00:00

I am writing a program that contains a JButton . Every time the button

  • 0

I am writing a program that contains a JButton. Every time the button is clicked, a new JTextField is added to a JPanel.

My problem is that, after the user has created all the JTextFields and filled them with information, I need to get the text of each field. How can I get access to the JTextFields when they are dynamically generated, as they don’t have instance names? Is there a better way to get the text of each one, without knowing their instance name.

Here is the code of the actionPerformed event of the JButton…

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JTextField x = new JTextField();
    x.setColumns(12);
    p.add(x); 
    validate();
}
  • 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-06-03T10:24:16+00:00Added an answer on June 3, 2026 at 10:24 am

    You say you want to get the text from each field. So, when you create the new instances x, why don’t you keep a collection of them, such as adding the JTextFields to an ArrayList?

    Alternatively, assuming that p is a JPanel, you should be able to get all the children, which would be the JTextFields that you’re adding. Try using getComponents() like so…

    Component[] children = p.getComponents();
    for (int i=0;i<children.length;i++){
        if (children[i] instanceof JTextField){
            String text = ((JTextField)children[i]).getText():
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains log entries for a program I'm writing. I'm
I'm writing a program that checks if a list contains sub-lists of unique length:
I'm writing a program that contains multiple packages in it. Each package has its
The program that I am writing needs to do this: read every line of
I'm writing a program that contains a generational garbage collector. There are just two
I am writing a program that takes one action if the command line contains
Im writing a program that should read input via stdin, so I have the
I am writing a program that generates a single large table of information. The
I am writing a program that needs to run a java.jar server. I need
I am writing a program that uses prints a hex dump of its input.

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.