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

Thread: Error when adding ActionListener to JButton

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error when adding ActionListener to JButton

    Hi, I'm having problems with my GUI java application. I get an error when I try to add an Aciton Listener to a button. I'm not sure if I am doing this the right way. Any help would be much appreciated.
    Here is my code:
    import javax.swing.*;
     
    import java.awt.event.*;
    import java.awt.*;
     
    public class JavaLabel extends JFrame implements ActionListener{
    	public static JLabel label;
     
    	public static void main(String[] args){
     
    		JLabel label;
    		JTextField edit;
    		JTextField noedit;
    		JLabel Entername;
    		JTextField name = null;
    		JButton click;
    		final String StoreName = "";
     
    		edit  = new JTextField();
    		name = new JTextField();
    		noedit = new JTextField("Enter Text: ");
    		label = new JLabel("label");
    		JFrame frame = new JFrame("frame");
    		Entername = new JLabel("enter name: ");
    		click  = new JButton("Okay");
    		click.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			if(e.getSource() == this){
    				StoreName = name.getText();
    				JOptionPane.showMessageDialog(null, StoreName);
    				System.exit(0);
     
    			}
    		}
     
    });
     
     
     
    		Entername.setBounds(100, 200, 120, 30);
    		name.setBounds(100, 250, 130, 30);
    		click.setBounds(100, 300, 60, 30);
     
    		Dimension size1 = edit.getPreferredSize();
    		Dimension size2 = noedit.getPreferredSize();
    		Dimension size3 = label.getPreferredSize();
    		edit.setBounds(100, 30, 100, size1.height);
    		noedit.setBounds(30, 30, size2.width, size2.height);
    		label.setBounds(200 - size3.width, 5, size3.width, size3.height);
     
    		noedit.setEditable(false);
    		frame.setLayout(null);
    		frame.setSize(400,400);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(label);
    		frame.add(edit);
    		frame.add(noedit);
    		frame.add(click);
    		frame.add(name);
    		frame.add(Entername);
     
    			}
     
    	}


    I get an error on line 4 with the JavaLabel.
    I also get an error in line 26 inside the public void actionPerformed. I get an error on the name.getText();

    Please any help would be great!

    THanks
    Last edited by grimrader22; November 9th, 2011 at 09:35 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Error when adding ActionListener to JButton

    Quote Originally Posted by grimrader22 View Post
    I get an error
    Since we don't read minds you will have to post the exact error messages you get.
    Improving the world one idiot at a time!

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Error when adding ActionListener to JButton

    Quote Originally Posted by grimrader22 View Post
    Hi, I'm having problems with my GUI java application. I get an error when I try to add an Aciton Listener to a button. I'm not sure if I am doing this the right way. Any help would be much appreciated.
    Here is my code:


    import javax.swing.*;
     
    import java.awt.event.*;
    import java.awt.*;
     
    public class JavaLabel extends JFrame implements ActionListener{
    	public static JLabel label;
     
    	public static void main(String[] args){
     
    		JLabel label;
    		JTextField edit;
    		JTextField noedit;
    		JLabel Entername;
    		JTextField name = null;
    		JButton click;
    		final String StoreName = "";
     
    		edit  = new JTextField();
    		name = new JTextField();
    		noedit = new JTextField("Enter Text: ");
    		label = new JLabel("label");
    		JFrame frame = new JFrame("frame");
    		Entername = new JLabel("enter name: ");
    		click  = new JButton("Okay");
    		click.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			if(e.getSource() == this){
    				StoreName = name.getText();
    				JOptionPane.showMessageDialog(null, StoreName);
    				System.exit(0);
     
    			}
    		}
     
    });
     
     
     
    		Entername.setBounds(100, 200, 120, 30);
    		name.setBounds(100, 250, 130, 30);
    		click.setBounds(100, 300, 60, 30);
     
    		Dimension size1 = edit.getPreferredSize();
    		Dimension size2 = noedit.getPreferredSize();
    		Dimension size3 = label.getPreferredSize();
    		edit.setBounds(100, 30, 100, size1.height);
    		noedit.setBounds(30, 30, size2.width, size2.height);
    		label.setBounds(200 - size3.width, 5, size3.width, size3.height);
     
    		noedit.setEditable(false);
    		frame.setLayout(null);
    		frame.setSize(400,400);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(label);
    		frame.add(edit);
    		frame.add(noedit);
    		frame.add(click);
    		frame.add(name);
    		frame.add(Entername);
     
    			}
     
    	}

    I get an error on line 4 with the JavaLabel.
    I also get an error in line 26 inside the public void actionPerformed. I get an error on the name.getText();

    Please any help would be great!

    THanks
    if(e.getSource() == this)

    I think the "this" would refer to your anonymous ActionListener class. Is that what you were hoping for?

    final String StoreName = "";

    I believe stuff that is declared final cannot be reinitialized.

    Therefore, if what I heard somewhere was correct, you cannot set it StoreName.getText().

    Why that value is final in the first place I can't figure out......

    As for the first one, why in the world are you extending JFrame if you never use any of the operations of that class?

    You're using your own JFrame variable. You basically don't need to extend it anyway based on the code you've posted.

    Also, you need to make the class itself have an actionPerformed() method in order to implement ActionListener. You aren't so it's saying your class isn't abstract and doesn't implement interface ActionListener or something like that, am I right?

    Again, I see no reason, given what you've posted, for your class to implement ActionListener personally, vs. for one of the Components like you're doing right now, anyway.
    Last edited by javapenguin; November 9th, 2011 at 10:10 PM.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error when adding ActionListener to JButton

    >> I get an error on line 4 with the JavaLabel.

    You get an error on line 4 because you have to implement the method of the interface "ActionListener"...

    >> I also get an error in line 26 inside the public void actionPerformed. I get an error on the name.getText();

    Before the line 26 you create an anonymous class "click.addActionListener(new ActionListener(){ ... });".
    Inside the anonymous class you cannot use local variables than you cannot access variable "name" in here "name.getText();".
    And If you correct this error you have another one because you cannot change the value of an final variable "StoreName = name.getText();"

    One simple manner for you resolve this problem (but not the best) you can declare "StoreName" and "name" together with "public static JLabel label;", "public static String StoreName;" and "public static JTextField name;"

    And delete this "extends JFrame implements ActionListener".

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. [SOLVED] error adding textboxes to panel
    By macko in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 20th, 2011, 06:50 AM
  3. JButton.... actionListener.... help!!!!....
    By eiramae in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 05:54 AM
  4. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 15
    Last Post: February 10th, 2011, 02:21 AM
  5. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM

Tags for this Thread