How to set backgound color of default panel from mouse event

Posted on 2008-11-12
Last Modified: 2013-11-23
This is a class assignment and I'm sorta stuck:
The following is a snippet from Panel class.   The assigment is to set the background color of the default panel to one color if the mouse enters over the panel and another upon it's exit.   I've identified the events but don't know how to reference the default content panel.

I'd appreciate any thoughts.

Thank you.




import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class CirclePanel extends JPanel {

    private final int WIDTH = 600,  HEIGHT = 400;

    private Circle circle;

    public CirclePanel() {

        // instantiate new object and add listeners

        // replaces addMouseListener(new CirclesListener());

        CirclesListener listener = new CirclesListener(); 



        setPreferredSize(new Dimension(WIDTH, HEIGHT));




    private class CirclesListener implements MouseListener, MouseMotionListener {

        public void mouseEntered(MouseEvent event) {

            // how to identify default content panel??

            // setBackground(Color.WHITE);


        public void mouseExited(MouseEvent event) {

            // how to identify default content panel??

            // setBackground(Color.BLUE);




Open in new window

Question by:RichardKline
    LVL 86

    Accepted Solution

    Your commented out code will work fine as part of an inner class of the JPanel
    LVL 86

    Expert Comment

    LVL 1

    Author Comment

    yeah... story of my life.  Never, ever, Ever look for the easy way.   *sigh*


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now