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

Thread: ClickListener cannot be resolved to a type

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default ClickListener cannot be resolved to a type

    Hi. I'm just starting to learn about inner classes. The following code is an example I followed from my book, but I still don't fully understand the explanation on lines 22 - 24:

    package samsExperiments;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class InnerClassTest extends JFrame{
     
    	public InnerClassTest() {
    		//code that sets up the frame
    		this.setTitle("TestFrame");
    		this.setSize(400,100);
    		this.setLocationByPlatform(true);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		JPanel panel = new JPanel();
    		this.add(panel);
     
    		//code that creates the button and adds the listener
    		JButton button1 = new JButton("Click me!");
    		//Next, we create an object from the inner ClickListener class and assign it
    		//to an ActionListener variable. This is possible because the ClickListener class
    		//implements the ActionListener interface and it's actionPerformed method:
    		ActionListener listener = new ClickListener();//ClickListener cannot be resolved to a type
    		button1.addActionListener(listener);
     
    		//display the frame window
    		panel.add(button1);
    		this.setVisible(true);
     
    		//the inner class that implements the listener
    		class ClickListener implements ActionListener{
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				System.out.println("The button was clicked!");
    			}
    		}//end of the inner class
     
    	}//end of the outer class
    }

    The error on line 25 says, "ClickListener cannot be resolved to a type". I'm used to seeing an object instantiation be created from a class such as "class x = new class()".

    Since when is it possible assign an object instance to a variable instead of creating one from a class? What is going on here?

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

    Default Re: ClickListener cannot be resolved to a type

    You are trying to instantiate the listener before it's defined. Did you really mean to put this method inside the constructor? You can fix this one of two ways.

    1. Move the inner class definition before you try to instantiate.
    2. Move the inner class definition outside of the constructor (but keep it within the main class.

    Regards,
    Jim

Similar Threads

  1. Object cannot be resolved to a type in JSP
    By archana.acharya in forum Java IDEs
    Replies: 1
    Last Post: September 4th, 2014, 10:49 PM
  2. [SOLVED] how to convert a input array of type Strings to a class type
    By adit in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 17th, 2014, 07:46 PM
  3. Getting error as "cannot be resolved to a type"
    By Prince76 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2013, 07:18 AM
  4. Error message "Type result cannot be resolved or is not a field"
    By asreall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2012, 08:40 AM
  5. cannot be resolved to a type
    By Teraphim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2010, 10:42 AM