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: Re: ActionListener help

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Angry Re: ActionListener help

    Quote Originally Posted by Junky View Post
    A solution is not to put any code into the actionPerfomed method other than a call to another method in the enclosing class. This method should have access to everything you need. Something like...
    class Foo {
        private JTextfield text;
        private JButton button;
     
        Foo() {
            text = new JTextField();
            button = new JButton();
            button.addACtionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    someMethod();
                }
            });
        }
     
        private void someMethod() {
            // do what you want here
        }
    }
    Above is what I'm trying do

    I have this and it works
    switchHLbut.addActionListener(new ActionListener()
    {    
     public void actionPerformed(ActionEvent e)
    	{
       if(setBg == true){
                switchHLbut.setText("ON");
                sbc.setBackground(Color.white);
                sbc.setText("");
                setBg = false;
              }
            else if(setBg == false){
                switchHLbut.setText("OFF");
                sbc.setBackground(Color.yellow);
                sbc.setText("");
                setBg = true;
                }
        	}
    });

    but I want to call a method within the Action Listener
     
    jbNew.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
        {   
            System.out.println(" inside jbNew Button");
            switchjbNew();   //calling a new method
        }    
      }); //error:reached end of file while parsing
     
    public void switchjbNew()   //error: illegal start of expression missing javadoc
    { 
        System.out.println("inside switchjbNew");
    }
    this was my attempt at calling a method from within the button ActionListener
    Failed


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ActionListener help

    Are you trying to code that method inside another method? That is not allowed. Put the method outside of the {}s that define the method the jbNew.addActionListener(new ActionListener(){ statement is in.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    JoeB (November 14th, 2013)

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    Quote Originally Posted by Norm View Post
    Are you trying to code that method inside another method? That is not allowed. Put the method outside of the {}s that define the method the jbNew.addActionListener(new ActionListener(){ statement is in.

    I thought it was outside of any methods, It's below the button.
    Thanks.
    I have something to work with now.

    --- Update ---

    I got it, I put it right after the closing } of the constructor before

    public static void main[]

Similar Threads

  1. Need help with ActionListener if-else
    By texasPI in forum Loops & Control Statements
    Replies: 4
    Last Post: September 6th, 2013, 07:18 PM
  2. ActionListener
    By ericgomez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 22nd, 2013, 12:25 PM
  3. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  4. Help with ActionListener please
    By knightmetal in forum AWT / Java Swing
    Replies: 3
    Last Post: August 23rd, 2011, 05:41 PM
  5. ActionListener Help?
    By Drag01 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 08:21 PM