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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:16:27+00:00 2026-05-28T05:16:27+00:00

I currently have a set of buttons which when pressed, update an element of

  • 0

I currently have a set of buttons which when pressed, update an element of an array with their value and then close the frame which contains them.

Currently, their actions are set out like this:

...
svwnb.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            input[5] = svwnb.getText();
            dftframe.setVisible(false);
        }
    });
    blypb.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            input[5] = blypb.getText();
            dftframe.setVisible(false);
        }
    });
    pw91b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            input[5] = pw91b.getText();
            dftframe.setVisible(false);
        }
    });
    b97db.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            input[5] = b97db.getText();
            dftframe.setVisible(false);
        }
    });
    pbepbesolb.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            input[5] = pbepbesolb.getText();
            dftframe.setVisible(false);
        }
    });
...

Is there a way of simplifying this into a simple method? I’ve got a feeling I’ll need to use the getSource() method but I have no experience in constructing an action method like this.

To be more succinct, I have an idea that the code will look something like the following pseudocode:

 public void actionPerformed(ActionEvent e){
     input[5] = e.getSource().getText();
     dftframe.setVisible(false);
 }

I just don’t know exactly how to generate this code.

  • 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-28T05:16:28+00:00Added an answer on May 28, 2026 at 5:16 am

    You can define the ActionListener in the following way:

    ActionListener commonListener = new ActionListener() {
    
        public void actionPerformed(ActionEvent e) {
            Object o = e.getSource();
            if (o instanceof JTextField) {
                JTextField textField = (JTextField)o;
                input[5] = textField.getText();
                textField.setVisible(true);
            }
        }
    };
    

    And use it by adding it to each component:

    svnwnb.addActionListener(commonListener);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an app which, when a button is pressed, and after a
I currently have my code set to disable the submit button when the field
I currently have speakers set up both in my office and in my living
I currently have Magento set up on a development server (remote, SSH access) and
I currently have a set of tabs, each tied to a fragment, and I've
Currently I have subversion set up so that when I make changes in Eclipse
I currently have CruiseControl.NET set up with SVN to automatically grab the code source
I currently have my PHP class variables set up like this: class someThing {
I am currently in C# and I have set Session variables on each page.
I currently have a MySQL dual master replication (A<->B) set up and everything seems

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.