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 8100583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:46:03+00:00 2026-06-05T22:46:03+00:00

Hello :) I am beginner in Java Swing and I can’t google solution for

  • 0

Hello 🙂 I am beginner in Java Swing and I can’t google solution for my problem. I have a JPanel and want to add JTextField(s) dynamically after pressing a JButton. And how can I getText() from them later? My code, commented part isn’t working properly.

Variable ‘counter’ counts how many fields I have in panel.

public class AppPanel extends JPanel {

    private JTextField tfData[];
    private JButton btAdd;
    private int counter = 1;

    public AppPanel() {
            setLayout(null);

            //tfData[counter] = new JTextField();
            //tfData[counter-1].setBounds(20, 20, 250, 20);
            //add(tfData[counter-1]);

            btAdd = new JButton("Add field");
            btAdd.setBounds(280, 20, 120, 20);
            btAdd.addActionListener(new alAdd());
            add(btAdd); 
    }

    class alAdd implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                    //tfData[counter] = new JTextField();
                    //tfData[counter].setBounds(20, 20+20*counter, 250, 20);
                    //add(tfData[counter]);
                    ++counter;
            }
    }
}
  • 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-05T22:46:05+00:00Added an answer on June 5, 2026 at 10:46 pm

    As you’re already storing references to your text fields, just use this array to query the text of the text fields:

    tfData[counter-1].getText();
    

    will show you the text of the last added text field.

    But you really should initialise your array before, otherwise you won’t be able to add any items to it. I think that was your main problem as you commented out your adding-code.

    // think about how many text fields you will need (here: 16)
    private JTextField tfData[] = new tfData[16];
    

    If you’re using arrays, watch for not breaking over its bounds. But better use a list as proposed in the comments before as it grows dynamically and you won’t have to deal with array bounds and can even skip counting (the list does that for you, too).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm a Django Beginner. I have a calendar that I want to be
hello i am a beginner in objective-c i have a nsstring like this Select
hello i'm a beginner java developer and this is one question in a list
Hi this is ROR beginner's question. I have creat controller.rb and view hello.rhtml following
I am a beginner C++ learner and I always have a problem on if
Hello friends i am a total beginner in c#. I want to read numbers
Hello bash beginner question. I want to look through multiple files, find the lines
I'm a complete beginner in JS, so pls have patience :) my.hello = function
i have a java beginner question: Parent.print() prints hallo in the console, but also
Hello I want to make simple shopping cart. I am a beginner in programming.

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.