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

Thread: ActionListener help

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ActionListener help

    I need help getting my action listener. I searched around and got a lot of help but I still can't quite get my issue resolved. It needs to perform an action when the user hits enter in a JTextField and I'm not even sure that I'm on the right track. I made notes where I am getting my errors and I believe that it is just a scope issue for the most part. It has been a while since I have wrote any code. Thanks in advance.


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class CurrencyConverter extends JFrame implements ActionListener{
    	double dollars;
    	double pounds;
     
    	public static void main(String[] args){
    		CurrencyConverter converter = new CurrencyConverter();
    		final JTextField dollarsField = new JTextField("", 25);//create JTextFields
    		dollarsField.addActionListener(GetDollarInputListener());//error at this line
    		final JTextField poundsField = new JTextField("", 25);
    		poundsField.addActionListener(GetPoundsInputListener());//error at this line
    		JFrame frame = new JFrame();//create JFrame
    		frame.setTitle("Currency Converter");//set frame properties
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);					
    		frame.setVisible(true);				
    		frame.add(converter.createGUI(dollarsField, poundsField));//call on createGUI to retrieve jpanel to create GUI and add it to the JFrame
    		frame.pack();
    		frame.setSize(350,110);
     
     
    	}//end main method
     
     
    	JPanel createGUI(JTextField dol, JTextField pou){
    		JPanel text = new JPanel(new GridLayout(2,1));
    		JPanel labels = new JPanel(new GridLayout(2,1));
    		JPanel complete = new JPanel(new BorderLayout());
     
    		JLabel dollars = new JLabel("Dollars:  ");
    		JLabel pounds = new JLabel("Pounds:  ");
    		labels.add(dollars);
    		text.add(dol);
    		labels.add(pounds);
    		text.add(pou);
    		complete.add(labels, BorderLayout.WEST);
    		complete.add(text, BorderLayout.CENTER);
     
    		return complete;		
    	}//end createGUI method
     
    	class GetDollarInputListener implements ActionListener{
    		public void actionPerformed(ActionEvent e){
     
    		}
    	}//end GetDollarInputListener class
    	class GetPoundsInputListener{
    		public void actionPerformed(ActionEvent e){
     
    			double poundsAmount = Double.parseDouble(converter.poundsField.getText());//error at this line
    		}
     
    	}//end GetPoundsInputListener class
    }//end CurrencyConverter class


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: ActionListener help

    dollarsField.addActionListener(new GetDollarInputListener());
    //and
    poundsField.addActionListener(new GetPoundsInputListener());
    Last edited by Brt93yoda; October 13th, 2010 at 03:59 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    Thanks for the reply. However that did not work and the errors didn't go away.

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: ActionListener help

    what's the error?

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    The Method GetDollarsField is undefined for the type CurrencyConverter. The error down at the bottom in my action listener class is: converter can not be resolved. I also forgot that I have an error where I declare my class at the top asking for an action listener since it implements it.

  6. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: ActionListener help

    I fixed as many errors as possible, but I still get the one when you add the action listener

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class CurrencyConverter extends JFrame {
        double dollars;
        double pounds;
     
        public static void main(String[] args){
            CurrencyConverter converter = new CurrencyConverter();
            JTextField dollarsField = new JTextField("", 25);//create JTextFields     
            dollarsField.addActionListener(new GetDollarInputListener());//error at this line
            JTextField poundsField = new JTextField("", 25);
            poundsField.addActionListener(new GetPoundsInputListener());//error at this line
            JFrame frame = new JFrame();//create JFrame
            frame.setTitle("Currency Converter");//set frame properties
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);                  
            frame.setVisible(true);             
            frame.add(converter.createGUI(dollarsField, poundsField));//call on createGUI to retrieve jpanel to create GUI and add it to the JFrame
            frame.pack();
            frame.setSize(350,110);
     
     
        }//end main method
     
     
        public JPanel createGUI(JTextField dol, JTextField pou){
            JPanel text = new JPanel(new GridLayout(2,1));
            JPanel labels = new JPanel(new GridLayout(2,1));
            JPanel complete = new JPanel(new BorderLayout());
     
            JLabel dollars = new JLabel("Dollars:  ");
            JLabel pounds = new JLabel("Pounds:  ");
            labels.add(dollars);
            text.add(dol);
            labels.add(pounds);
            text.add(pou);
            complete.add(labels, BorderLayout.WEST);
            complete.add(text, BorderLayout.CENTER);
     
            return complete;        
        }//end createGUI method
     
        private class GetDollarInputListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
     
            }
        }//end GetDollarInputListener class
       private class GetPoundsInputListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
     
                double poundsAmount = Double.parseDouble(converter.poundsField.getText());//error at this line
            }
     
        }//end GetPoundsInputListener class
    }//end CurrencyConverter class

  7. #7
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: ActionListener help

    I got it to work, you had to make the classes static.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class currencyConverter extends JFrame {
        static double dollars;
        static double pounds;
        static JTextField poundsField = new JTextField();
        static JTextField dollarsField = new JTextField();
        public static void main(String[] args){
            currencyConverter converter = new currencyConverter();
            dollarsField = new JTextField("", 25);//create JTextFields     
            dollarsField.addActionListener(new GetDollarInputListener());//error at this line
            poundsField = new JTextField("", 25);
            poundsField.addActionListener(new GetPoundsInputListener());//error at this line
            JFrame frame = new JFrame();//create JFrame
            frame.setTitle("Currency Converter");//set frame properties
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);                  
            frame.setVisible(true);             
            frame.add(converter.createGUI(dollarsField, poundsField));//call on createGUI to retrieve jpanel to create GUI and add it to the JFrame
            frame.pack();
            frame.setSize(350,110);
     
     
        }//end main method
     
     
        public static JPanel createGUI(JTextField dol, JTextField pou){
            JPanel text = new JPanel(new GridLayout(2,1));
            JPanel labels = new JPanel(new GridLayout(2,1));
            JPanel complete = new JPanel(new BorderLayout());
     
            JLabel dollars = new JLabel("Dollars:  ");
            JLabel pounds = new JLabel("Pounds:  ");
            labels.add(dollars);
            text.add(dol);
            labels.add(pounds);
            text.add(pou);
            complete.add(labels, BorderLayout.WEST);
            complete.add(text, BorderLayout.CENTER);
     
            return complete;        
        }//end createGUI method
     
        private static class GetDollarInputListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
     
            }
        }//end GetDollarInputListener class
       private static class GetPoundsInputListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
     
                double poundsAmount = Double.parseDouble(currencyConverter.poundsField.getText());//error at this line
            }
     
        }//end GetPoundsInputListener class
    }//end CurrencyConverter class
    Last edited by Brt93yoda; October 13th, 2010 at 04:16 PM.

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    Awesome, thanks for all of the help. I will have to test it out a little later since I am in class but i will let you know if I have any more trouble. Thats a huge load off of my shoulders.

  9. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    Everything worked great after another tweak or two so thanks again. Now I just have to get the action listener to respond to the enter key.

  10. #10
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: ActionListener help

    It worked fine for me without tweaking, and it responded to enter.

  11. #11
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: ActionListener help

    Quote Originally Posted by Brt93yoda View Post
    I got it to work, you had to make the classes static.
    No no no no no!

    You have to create an instance of the class and call the (not static) methods on the instance.

    Java is an object oriented language.

    db

  12. #12
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: ActionListener help

    Quote Originally Posted by Darryl.Burke View Post
    No no no no no!

    You have to create an instance of the class and call the (not static) methods on the instance.

    Java is an object oriented language.

    db
    oh? ok? My bad I guess.

  13. #13
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    Yea, i accidentally messed something up regarding the enter key but I got everything to work fine.

  14. #14
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    And thankyou for the advice darryl, I'll be sure to remember that and try it out next time.

Similar Threads

  1. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 11:25 AM
  2. ActionListener Help?
    By Drag01 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 08:21 PM
  3. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 07:21 PM
  4. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 10:00 AM
  5. SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
    By beginning2Understand in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2009, 12:42 AM