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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:40:34+00:00 2026-06-01T18:40:34+00:00

I know that you can create a button by typing JButton x= new JButton(Something);

  • 0

I know that you can create a button by typing

JButton x= new JButton("Something");
x.addActionListener(this);

But how do I make an actionlistener so that the button creates a textfield fo a user to provide input….and how do I read the text from that textbox?

  • 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-01T18:40:35+00:00Added an answer on June 1, 2026 at 6:40 pm

    Swing has no such animal as a textbox — do you mean JTextField? If so,…

    • You would create a JTextField by simply doing just that — new JTextField()
    • Then would add it to your GUI by calling add(...) on an appropriate container such as a JPanel.
    • You would then read the text by simply calling getText() on it, and the JTextField tutorials will explain all of this.
    • You will also want to read the tutorial on the layout managers since the container that accepts the JTextField must have a layout that can accommodate the new component.
    • You will also want to call revalidate() and repaint() on the container after adding or removing components so that the containers layout manager will know to update its layout and repaint itself.

    This is just a general gist of what needs to be done. If you need more specific advice, please tell us the details of your problem, what you’ve tried so far, and what has worked or failed.

    Edit
    You ask:

    But how do I do this so that the textField is a “pop up” rather than an addition to the current container. I have it so that it adds to the current container…but that is not what I need.

    • The easiest way to do this is by displaying a JOptionPane.showInputDialog(…).

    For example:

      // myGui is the currently displayed GUI
      String foo = JOptionPane.showInputDialog(myGui, "Message", "Title",
            JOptionPane.PLAIN_MESSAGE);
      System.out.println(foo);
    

    This would look like so:
    enter image description here

    • A more complex way of doing this is to create a GUI and display it in a JOptionPane, and the query the GUI for its contents.

    For example:

      JTextField fooField = new JTextField(15);
      JTextField barField = new JTextField(15);
      JPanel moreComplexPanel = new JPanel(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.insets = new Insets(5, 5, 5, 5);
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.anchor = GridBagConstraints.WEST;
      moreComplexPanel.add(new JLabel("Foo:"), gbc);
      gbc.gridx = 1;
      gbc.anchor = GridBagConstraints.EAST;
      moreComplexPanel.add(fooField, gbc);
      gbc.gridx = 0;
      gbc.gridy = 1;
      gbc.anchor = GridBagConstraints.WEST;
      moreComplexPanel.add(new JLabel("Bar:"), gbc);
      gbc.gridx = 1;
      gbc.anchor = GridBagConstraints.EAST;
      moreComplexPanel.add(barField, gbc);
    
      int result = JOptionPane.showConfirmDialog(myGui, moreComplexPanel, 
            "Foobars Forever", JOptionPane.OK_CANCEL_OPTION, 
            JOptionPane.PLAIN_MESSAGE);
      if (result == JOptionPane.OK_OPTION) {
         System.out.println("foo = " + fooField.getText());;
         System.out.println("bar = " + barField.getText());;
      }
    

    Which would look like:
    enter image description here

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

Sidebar

Related Questions

I know that we can't overload operator with other meaning, we can't create new
I know that horizontal partitioning...you can create many tables. How can you do this
I am New for the technology. I want to know that Can we create
I'm using python 3.1.1. I know that I can create byte objects using the
I know that if i have rootViewController that start with tabBar i can create
I wish to know is there any way that I can create the threads
On iOS, I know that we can evaluate a trust with SecTrustEvaluate(). To create
Does anyone know of any software that can be used to create Agile User
I would like to know how to create a php function that can be
Does anyone know how I can Dynamically create an installation package that installs files

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.