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

Thread: method not passing across actionlistener interface

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

    Default method not passing across actionlistener interface

    I need to make a basic GUI with three buttons that changes the background to either red, blue, or green. I have the interface set up just fine, but I cannot get the actionlistener code to work. The error is specifically on the ColorField.setColor in the three listener classes: "ColorField cannot be resolved".

    import javax.swing.*;

    class ColorSelector extends JFrame
    {
    public ColorSelector()
    { setTitle("My Empty Frame");
    setSize(300,200); // default size is 0,0
    setLocation(10,200); // default is 0,0 (top left corner)
    }
    }


    import java.awt.event.ActionListener;
    import javax.swing.*;

    public class ColorSelectorTester {

    public static void main(String[] args) {
    JFrame frame = new ColorSelector();
    frame.show();

    JButton Rbutton = new JButton("Red");
    JButton Gbutton = new JButton("Green");
    JButton Bbutton = new JButton("Blue");
    JLabel ColorField = new JLabel();
    ColorField.setOpaque(true);


    JPanel panel = new JPanel();
    panel.add(ColorField);
    panel.add(Rbutton);
    panel.add(Gbutton);
    panel.add(Bbutton);
    frame.add(panel);

    ActionListener Rlistener = new RButtonListener();
    Rbutton.addActionListener(Rlistener);
    ActionListener Glistener = new GButtonListener();
    Rbutton.addActionListener(Glistener);
    ActionListener Blistener = new BButtonListener();
    Rbutton.addActionListener(Blistener);
    }
    }


    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;


    public class RButtonListener implements ActionListener{


    public void actionPerformed(ActionEvent Rbutton)
    { ColorField.setColor(Color.red);
    }

    }


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

    Default Re: method not passing across actionlistener interface

    You have declared ColorField inside the main method of the ColorSelectorTester class and then trying to access it from the RButtonListener class. You cannot do this. You can only access a variable within the scope it is declared in. You will need to rethink your design.
    Improving the world one idiot at a time!

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

    Default Re: method not passing across actionlistener interface

    Yeah I can, because it's going through an interface, I just dont know how to declare it the RIGHT way!

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

    Default Re: method not passing across actionlistener interface

    err no you can't. Unless you are using a different version of Java to everyone else on the planet.
    Improving the world one idiot at a time!

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

    Default Re: method not passing across actionlistener interface

    Then how should I be doing it? Because I am completely stumped.

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

    Default Re: method not passing across actionlistener interface

    For starters I would move most of the code from your main method into the GUI class. Then you can make your Listener classes inner classes of the GUI class. This will allow them to have access to instance variables.
    Improving the world one idiot at a time!

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: method not passing across actionlistener interface

    Thread moved to the Swing forum. And for future reference, please wrap your code in the [highlight=java][/highlight] tags

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

    Default Re: method not passing across actionlistener interface

    Wrap my code in tags? Huh?

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: method not passing across actionlistener interface

    Quote Originally Posted by flamewolf393 View Post
    Wrap my code in tags? Huh?
    Yes...when done so code is actually comprehensible to a human being. Typing:

    [highlight=java]your code here[/highlight]

    Results in

    Your code here

    With spacing retained, and highlights to boot

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: method not passing across actionlistener interface

    Quote Originally Posted by flamewolf393 View Post
    Yeah I can, because it's going through an interface, I just dont know how to declare it the RIGHT way!
    You can not. If it's coming from the interface, that interface is not implemented by RButtonListener, is it?

Similar Threads

  1. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM
  2. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM
  3. interface class ActionListener
    By NightFire91 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 17th, 2010, 10:42 AM
  4. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 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

Tags for this Thread