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 5 of 5

Thread: Creating a Listeners class

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a Listeners class

    So I've broken out my JFrame controls into their own class called Controls that extends the main class.

    Now I'm trying to break out the Action Listeners into a Listeners class.

    This code runs in
    public static void main(String[] arguments) throws IOException {}:

    Controls.btnClear.addActionListener(new ActionListener()
    {
    	public void actionPerformed(ActionEvent e)
    	{
    		Controls.panel.remove(Controls.label);
    		Controls.panel.repaint();
    	}
    });

    I have the Listeners class as:
    public class Listeners extends Controls implements ActionListener {}

    The question is, how do I get that btnClear (it clears an image in a panel) to work when I move it to the Listeners class?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,897
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Creating a Listeners class

    get that btnClear (it clears an image in a panel) to work
    Please explain what "to work" means.

    Note: btnClear and panel should not be coded as static.

    Why does Listeners extend Controls?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Listeners class

    I have a panel that an image is displayed in via a label. Clicking btnClear clears the image from the panel by removing the label. So the action listener code for btnClear works when it's in main. So how do I get that code to work inside of a Listeners class?

  4. #4
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Listeners class

    I got it figued out. In the Controls class where I am creating the button I create the ActionListener:

    btnClear.addActionListener(new Listeners().new btnClearListener());

    And then in my Listeners class I create the individual control listeners:

    public class btnClearListener implements ActionListener {
     
            public void actionPerformed(ActionEvent e) {
                Controls.panel.remove(Controls.label);
                Controls.panel.repaint();
            }
     
    }

    I suppose I could use a single Listener and test for whatever control is being used but that doesn't seem very simple or efficient.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,897
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Creating a Listeners class

    Using static methods is a poor way to write OOP code. Use references to an instances of the class and have getter methods that return the field to be worked on.

    Classnames should start with an Uppercase letter.


    Try writing the code without any static methods.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] creating an iterator without creating a class for it
    By Lenjaku in forum Collections and Generics
    Replies: 6
    Last Post: January 22nd, 2014, 02:17 AM
  2. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  3. [SOLVED] Class Creating Help
    By pitchblack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 10:25 PM
  4. Action Listeners and Key Listeners Help
    By xctive in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 08:27 AM
  5. Help with creating a class
    By cdawg_2010 in forum Loops & Control Statements
    Replies: 4
    Last Post: November 1st, 2010, 07:04 AM