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

Thread: Looking for help with assignment

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Looking for help with assignment

    Hey guys I'm having problems with a Java assignment that I've been working on for weeks. Hopefully someone can steer me in the right direction. My assignment is to make the following changes to the program below:


    (I already finished this but the first part is too...) change the blueButton so that it's event handling is done by an anonymous inner class. The ActionListener interface will serve as the "base class" for the anonymous inner class (this is okay, even though interfaces cannot be instantiated). Within the anonymous class definition you will specify the required event-handling method, which contains a couple of lines of code to change the color.

    Then change the yellowButton so that an instance of a "top level" class (not an inner class) which you create, called ButtonListener, will do the event handling for it. You will need to define the class, and also create an instance of it, passing to the constructor the reference to the ButtonPanel (the "this" object). The constructor should save this reference in an instance variable. This reference is needed for a "callback" when the event is being handled. That is, the calls to setBackground() and repaint() should be done on the ButtonPanel object, not the ButtonListener object. After all, your listener object has no background - it's not even a visible object! It needs to set its creator object's background. That's why you saved the reference to the ButtonPanel in the ButtonListener's constructor.

    and last, change the redButton so that it uses an named "inner" class to the ButtonPanel. This is very handy, since the setBackground and repaint methods can both use the implied object of "this" (the ButtonPanel). If the listener is an inner class, then it automatically has access to its enclosing class' "this". So with the inner class, there is no need to pass the ButtonPanel's reference as with the "top-level" class. Of course, you still need to create an instance of the inner class, and register it as the listener for the redButton.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class ButtonPanel extends JPanel
    implements ActionListener
    {
        public ButtonPanel()    //constructor
        {
            yellowButton = new JButton("Yellow");
            blueButton = new JButton("Blue");
            redButton = new JButton("Red");
     
            add(yellowButton);
            add(blueButton);
            add(redButton);
     
            yellowButton.addActionListener(this);
            blueButton.addActionListener(this);
            redButton.addActionListener(this);
        }
     
        public void actionPerformed(ActionEvent evt)    //ActionListener method
        {
            Object source = evt.getSource();
            Color color = getBackground();
            if (source == yellowButton) color = Color.yellow;
            else if (source == blueButton) color = Color.blue;
            else if (source == redButton) color = Color.red;
            setBackground(color);
            repaint();
        }
     
        private JButton yellowButton;
        private JButton blueButton;
        private JButton redButton;
    }
     
    class ButtonFrame extends JFrame
    {
        public ButtonFrame()
        {
            setTitle("Button Test");
            setSize(300, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container contentPane = getContentPane();
            contentPane.add(new ButtonPanel());
        }
    }
     
    public class ButtonTest
    {
        public static void main(String[] args)
        {
            JFrame frame = new ButtonFrame();
            frame.show();
        }
    }

    Here is my code. I'm stuck at the second set of instructions with the yellowButton and ButtonListener class. I made comments to show everything I was trying to do but I honestly have no clue what I'm doing. My compile errors are below. I changed up the code a bit from the original because I'm used to seeing the main method first and seeing class variables declared at the top instead of the bottom.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class ButtonTest2
    {
        public static void main(String[] args)
        {
            JFrame frame = new ButtonFrame();
            frame.show();
        }
    }
     
    class ButtonListener implements ActionListener    //"top-level" class that implements the ActionListener interface for the event handling.
    {
        private ButtonPanel thePanel;   //object variables declared
        private ButtonPanel Pan;
        private ButtonListener l;
     
        public ButtonListener(ButtonPanel Pan)    //constructor that passes the reference to the ButtonPanel object (the "this" object)
        {
            thePanel = Pan;   //instance varible to save reference to theButtonPanel object
     
            ButtonListener l = new ButtonListener(thePanel);  //new ButtonListener object to do the event handling. I am referencing to the ButtonPanel object in the parentheses.
     
            l.addActionListener(); //call to event handler
     
        }
     
        public void actionPerformed(ActionEvent event)    //ActionListener method to do the event handling
        {
            l.setBackground(Color.yellow);
            l.repaint();
        }
     
    }
     
    class ButtonPanel extends JPanel
    {
        private JButton blueButton;
        private JButton yellowButton;
        private JButton redButton;
     
        public ButtonPanel()
        {
            JButton yellowButton = new JButton("Yellow");
            JButton blueButton = new JButton("Blue");
            JButton redButton = new JButton("Red");
     
            add(yellowButton);
            add(blueButton);
            add(redButton);
     
            blueButton.addActionListener(new ActionListener()    //annonymous class to perform the event handling for the blueButton
            {
                public void actionPerformed(ActionEvent event)
                {
                    Object blueButton = event.getSource();
                    setBackground(Color.blue);
                    repaint();
                }
            });
        }
    }
     
    class ButtonFrame extends JFrame
    {
        public ButtonFrame()
        {
            setTitle("Button Test");
            setSize(300, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container contentPane = getContentPane();
            contentPane.add(new ButtonPanel());
        }
    }

    Here are the errors
    java:33: cannot find symbol
    symbol  : method addActionListener()
    location: class ButtonListener
            l.addActionListener();
             ^
    java:39: cannot find symbol
    symbol  : method setBackground(java.awt.Color)
    location: class ButtonListener
            l.setBackground(Color.yellow);
             ^
    java:40: cannot find symbol
    symbol  : method repaint()
    location: class ButtonListener
            l.repaint();
             ^
    Note: ButtonTest2.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    3 errors


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

    Default Re: Looking for help with assignment

    The variable l is a ButtonListener (which is an ActionListener). This class does not have an addActionListener method. Why are you trying to add an ActionListener to an ActionListener anyway? It doesn't make sense. You add ActionListeners to buttons and other similar components.
    Improving the world one idiot at a time!

Similar Threads

  1. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  2. Assignment
    By Polly2010 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 30th, 2010, 03:42 PM
  3. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 04:20 PM
  4. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  5. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 09:44 PM