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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:20:17+00:00 2026-06-02T18:20:17+00:00

I have a class which contains a textfield and within that class an inner

  • 0

I have a class which contains a textfield and within that class an inner class containg the action performed method. On entering text and pressing enter, I would like the text to be assigned to a String “s” outside of the inner class that implements Action Listener. I want to use that string in another class.

public class Peaches extends JFrame {
    private JTextField item1;

    public Peaches() {
        super("the title");
        setLayout(new FlowLayout());        
        item1 = new JTextField(10);
        add(item1);

    thehandler handler = new thehandler(); //object of thehandler inner class
    item1.addActionListener(handler);      

    // I want to use the textfield content here, or in other classes, updated
    // with the new text everytime i hit enter after entering new text in the textfield 

    //String s = the content of the textfield
}

private class thehandler implements ActionListener { // inner class
    @Override
    public void actionPerformed(ActionEvent event) {
        String string = "";           
        string = String.format(event.getActionCommand());            
        JOptionPane.showMessageDialog(null, string);           
    }
}

Is there any way of doing this? It must be possible to use the input of a text field in other parts of your program. I hope I make myself clear, thanks all.

EDIT
Thanks for the replies, so making the variable string a class variable instead of declaring it in the constructor allows the whole class to access it, simple but something i just didnt understand. Thanks Marko

  • 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-02T18:20:19+00:00Added an answer on June 2, 2026 at 6:20 pm

    I suggest you do it like this:

    public class Peaches extends JFrame {
      private JTextField item1;
      private String string;
    
      public Peaches() {
        super("the title");
        setLayout(new FlowLayout());
        item1 = new JTextField(10);
        add(item1);
        item1.addActionListener(new ActionListener() {
          @Override public void actionPerformed(ActionEvent event) {
            string = String.format(event.getActionCommand() );
            JOptionPane.showMessageDialog(null, string);
        }});
      }
    }
    

    This uses an anonymous class, simpler than having dozens of inner classes around. Plus with these you can make closures (use local variables declared outside the anonymous class). And your string is there outside, so you can access it.

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

Sidebar

Related Questions

I have an abstract class which contains method for setting header text. It looks
I have a class which contains a static collection that can be used across
The problem: I have a class which contains a template method execute which calls
I have a class which contains a few boost::numeric::ublas::matrix's within it. I would like
I have TextField instance called inputWord which contains no text on the first frame.
I have a class which contains this method /** * Uses the native RegExp
I have a class which contains a generic dictionary: protected Dictionary<K,T> Data { get;
I have a class which contains 5 properties. If any value is assingned to
I have a ProductViewModel class which contains different properties. Then I have a ProductDetailsViewModel
I have a parent class which contains a child object. I am using set

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.