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: Action not working

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Angry Action not working

    Hello, i am trying to make a program with a button that outputs Hello World and the error tells me this... Multiple markers at this line
    - firstButton cannot be resolved to a variable
    - The method getSorce() is undefined for the type

    Here is the code
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class jframeTest {
    public static void main(String args[]) {
     
    	JFrame emptyWindow = new JFrame("Hello World");
    	emptyWindow.pack();
    	emptyWindow.setSize(550, 750);
    	emptyWindow.setResizable(false);
    	emptyWindow.setLocationRelativeTo(null);
    	emptyWindow.setVisible(true);
    	emptyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	JPanel panelOne = new JPanel();
    	JButton firstButton = new JButton("Hello World");
    	emptyWindow.add(firstButton);
    	emptyWindow.add(panelOne);
    	panelOne.add(firstButton);
        firstButton.addActionListener(new Action());
     
    	}
     
    public static class Action implements ActionListener {
     
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSorce()==firstButton){
    			System.out.println("It Works");
    		}
     
    	}
     
    }
     
     
    }


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Action not working

    firstButton cannot be resolved to a variable
    That error is very descriptive. If it states that the it cannot be resolved to a variable, that
    means one of two things: Either it was not created, or you did create it, but have it in the
    wrong scope. Check to see if the variable exists. After that, check if it was declared in a
    scope that allows it to be accessed.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Action not working

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Please observe Java's naming conventions which call for class names to begin with capital letters.

    You also have a typo at line 30:

    if(e.getSorce()==firstButton)

    Update:

    AND, this statement has a purpose:

    emptyWindow.pack();

    You should read the API to understand its purpose, and you may also then see that its purpose is only realized AFTER components have been added. Order often matters.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    812d (July 18th, 2014)

Similar Threads

  1. Action Listeners within and Action Listener on Panels
    By avidlearner in forum AWT / Java Swing
    Replies: 3
    Last Post: April 8th, 2014, 12:21 PM
  2. [SOLVED] Anonymous inner class with Action Listener and Abstract Action
    By shakes6791 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 13th, 2013, 03:52 PM
  3. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  4. action listener not working help
    By pranavk26 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: April 3rd, 2013, 08:49 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM

Tags for this Thread