Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Confused about ActionListener syntax

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Confused about ActionListener syntax

    Hi,

    I'm going through java swing tutorials here, and there's a line I don't understand:

    JButton button = new JButton("Click me");
     
    button.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
    		tf.setText("Welcome to swing");
    	}
    });

    Can someone please explain to me how you can define a method there? I've not come across this sort of definition before. Thanks

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Confused about ActionListener syntax

    That particular construct is known as an anonymous class. You can specify a class or an interface and it will construct a class in its place. I would advise against using it for very large classes. It can get cumbersome and hard to read.

    And as of Java 8 you can use a Lambda expression in place of an anonymous class or class reference.

              JButton b = new JButton();
    	  b.addActionListener((event)->System.out.println("Button Pushed"));
    	  b.doClick();  // triggers event and prints 'Button Pushed'


    Regards,
    Jim

  3. The Following User Says Thank You to jim829 For This Useful Post:

    dboxall123 (October 5th, 2018)

  4. #3
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Confused about ActionListener syntax

    Great, I'll read up on anonymous classes and lambda expressions, thanks Jim

Similar Threads

  1. Is this syntax possible?
    By PC_flea in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2014, 07:15 AM
  2. Syntax error!!!
    By Gerock7 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: April 22nd, 2014, 07:14 PM
  3. [SOLVED] Syntax Question...
    By Praetorian in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 8th, 2014, 04:54 AM
  4. What exactly is the syntax for println()
    By javaiscool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2013, 09:02 PM
  5. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM