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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:24:45+00:00 2026-06-17T09:24:45+00:00

@Override public void actionPerformed(ActionEvent e) { if (e.getSource() == thirdBtn) { //System.out.println(Third Button Click);

  • 0
  @Override
  public void actionPerformed(ActionEvent e) {
      if (e.getSource() == thirdBtn) {
          //System.out.println("Third Button Click");
          System.out.println(e.getSource()+" Click");
      }
  }

In the code above, I was wondering if instead of doing this:

//System.out.println("Third Button Click");

if I could do something like this:

System.out.println(e.getSource()+" Click");

However the code outputs:

BlackJack.OverBoard$BlackJackButton[,440,395,100x25,alignmentX=0.0,alignmentY=0.5,
    border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@7a3d8738,
    flags=16777504,maximumSize=,minimumSize=,preferredSize=,
    defaultIcon=,disabledIcon=,disabledSelectedIcon=,
    margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],
    paintBorder=false,paintFocus=true,
    pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,
    text=Change,defaultCapable=true] Click

I don’t want this, I want to know how to get the JButton name and output it on click.

EDIT:

Some people are confused. When I say “name” (maybe that’s the wrong word for it), I meant say you initialize a JButton

JButton btnExample = new JButton();

I want it so that when you click the button, it outputs btnExample in the console.

  • 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-17T09:24:46+00:00Added an answer on June 17, 2026 at 9:24 am

    You can cast to a JComponent if you know that only JComponents will be the return value of e.getSource() I’m using JComponent as the cast since it gives more flexibility. If you’re only using JButtons, you can safely cast to a JButton instead.

      @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == thirdBtn) {
                        //System.out.println("Third Button Click");
                        System.out.println(((JComponent) e.getSource()).getName()+" Click");
                    }
                }
    

    Feel free to replace getName() with getText(), depending on what exactly you need.

    Also, the == operator should only be used to compare Object references, so consider casting to a JComponent from the beginning and using .equals() on the names or text.

    Edit
    You can’t output the name of the variable, but you can set the name/text of the JComponent.
    Eg

    JButton btnExample = new JButton();
    btnExample.setName("btnExample");
    

    Or if you want “btnExample” to actually be displayed on the button:

    JButton btnExample = new JButton();
    btnExample.setText("btnExample");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

btnnew.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { System.out.println(Hello); packetListener.listener(); }
grass.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { pm = false; if(pm=false){ drawGrassBlock(x1,
I have: class CustomerActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent event) { JComboBox
in my GUI i have the @Override public void actionPerformed(ActionEvent ae) { state =
Code: outerMethod { @Override public void run() { innerMethod throws IOException } } Method
button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { db.update(); } }); On line
I have this code: @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0, 0,
Eclipse 3.5 indents this code correctly: addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e)
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); savedInstanceState.putString(foo, bar); } @Override public void
@Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); // providers data // db.execSQL(insert into //

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.