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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:33:03+00:00 2026-05-27T00:33:03+00:00

What is getSource? and what does it return? and what is getActionCommand() and what

  • 0

What is getSource? and what does it return?

and what is getActionCommand() and what does it return??

I am getting confused between these two can anyone give or differentiate them to me? what’s the use of getSource and getActionCommand() in UI’s? specifically TextField or JTextField?

  • 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-27T00:33:04+00:00Added an answer on May 27, 2026 at 12:33 am

    Assuming you are talking about the ActionEvent class, then there is a big difference between the two methods.

    getActionCommand() gives you a String representing the action command. The value is component specific; for a JButton you have the option to set the value with setActionCommand(String command) but for a JTextField if you don’t set this, it will automatically give you the value of the text field. According to the javadoc this is for compatability with java.awt.TextField.

    getSource() is specified by the EventObject class that ActionEvent is a child of (via java.awt.AWTEvent). This gives you a reference to the object that the event came from.

    Edit:

    Here is a example. There are two fields, one has an action command explicitly set, the other doesn’t. Type some text into each then press enter.

    public class Events implements ActionListener {
    
      private static JFrame frame; 
    
      public static void main(String[] args) {
    
        frame = new JFrame("JTextField events");
        frame.getContentPane().setLayout(new FlowLayout());
    
        JTextField field1 = new JTextField(10);
        field1.addActionListener(new Events());
        frame.getContentPane().add(new JLabel("Field with no action command set"));
        frame.getContentPane().add(field1);
    
        JTextField field2 = new JTextField(10);
        field2.addActionListener(new Events());
        field2.setActionCommand("my action command");
        frame.getContentPane().add(new JLabel("Field with an action command set"));
        frame.getContentPane().add(field2);
    
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(220, 150);
        frame.setResizable(false);
        frame.setVisible(true);
      }
    
      @Override
      public void actionPerformed(ActionEvent evt) {
        String cmd = evt.getActionCommand();
        JOptionPane.showMessageDialog(frame, "Command: " + cmd);
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I do a return in an _init*() function in my bootstrap what does
Can someone explain why the HashMap acts like it does in this example: Simple
How can I use ClassLoader.getResources() to find recursivly resources from my classpath? E.g. finding
How can I get source code off the internet using SVN? I'm trying to
I know the file needs to be where the getClass().getResource(filename) can find it, but
First time posting here, will try to be succinct. This is a classic 'can't
I'm trying to use ServletContext.getResource to retrieve a java.net.url reference to an image file
I use Eclipse to write the code and I get a red underline at
This code does returns null InputStream as well as null URL. Why is that?
It's me again and I just can't seem to get this code to work.

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.