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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:03:06+00:00 2026-06-08T08:03:06+00:00

# In the problem which i am posting,in this i am unable to understand

  • 0

# In the problem which i am posting,in this i am unable to understand the code mentioned on line 1 and line 2, As what i know about them is that they are used to set the action listener for the button but the thing which is most confusing for me is that in the syntax sated on the line 1 and line 2 as, {JB1.addActionListener(this)} in this what is the role of “this”…..so please tell the basic behind this.And how this whole syntax works…in detail. #

 import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.*;



    public class frametest1_1 implements ActionListener
    {
        JLabel JL;
        public frametest1_1()
        {
            //create a JFrame container
            JFrame JF=new JFrame("A BUTTON");

            //Frame Layout This is contained in Java .awt.*;  "ON USING THIS OPTION OUR BUTTON AND OTHER COMPONENT ARE ADJUSTED IN THE FRAME AUTOMATICALLY"
            JF.setLayout(new FlowLayout());

            //set the size of the container
            JF.setSize(200, 200);

            //set visible
            JF.setVisible(true);

            //make button
            JButton JB1=new JButton("FIRST");
            JButton JB2=new JButton("SECOND");

            //add button to the JFrame container
            JF.add(JB1);
            JF.add(JB2);

            //Create and add Label to the JFrame container
            JL=new JLabel("PRESS A BUTTON");
            JF.add(JL);

            //set action command :now this will help in determining that which button is presses, is it FIRST or SECOND
            JB1.setActionCommand("one");
            JB2.setActionCommand("two");

            //The action responded is added to the actionlistener
            JB1.addActionListener((ActionListener) this); // line 1
            JB2.addActionListener((ActionListener) this); // line 2
        }   

        public void actionPerformed(ActionEvent ae) 
        {

            if(ae.getActionCommand().equals("one"))
                JL.setText("First Button Pressed");     // to set text on the label
            else
                JL.setText("Second button Pressed");    // to set the text on the label

        }
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new frametest1_1();
                }
            });
        }
    }
  • 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-08T08:03:08+00:00Added an answer on June 8, 2026 at 8:03 am

    1. Consider that Listener is someone who reacts to some action.

    2.ActionListener is an Interface which has a call back method, named actionPerformed, inside which is the code that will run when certain action is done on the controller.

    3. This line JB1.addActionListener((ActionListener) this); means as follows

       JB1 - Button
       ActionListener - Interface
       addActionListener - Registering the Button with the Listener.
    

    4. addActionListener will bind/register the Button with the Listener (here its ActionListener).

    5. In an MVC architecture Button is the controller, when certain action is done on it, then who all are to be informed to do certain things is done by registering it to the listener.

    6. And this Listener will have the callback method which will be overridden by the class that implements the listener.

    7. Moreover in your example, you can also do it like this…
    JB1.addActionListener(this);

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

Sidebar

Related Questions

I don't really know what's causing this problem but my program, which is supposed
I have a problem which I don't really know how to solve. I have
This question has been answered. The problem was that myEventMoveTrainManaul() was being called from
I'm posting this on behalf of the 30 or so projects that seem to
Yes, I know that general forms of this question have been asked time and
I have a small problem which i can't seem to figure out. I tried
I've got a problem which seems to be a real nut. I am using
I have a problem which makes me crazy. I think it is very easy
Got a problem which to me make no sense. So here goes: I have
I have another weird problem which I have not been able to solve in

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.