Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2006

Tip: Looking for answers? Try searching our database.

MouseListener - Changing background color

Thread view: 
Rick - 28 Nov 2006 07:50 GMT
Here is the code I have.  Basically all I want to do is change the
background of the frame to black when I click the mouse (mouseClicked)
and change it back to white when I release the mouse (mouseReleased).
I can't access frame directly to do frame.setBackground(Color.BLACK),
and that is where I am stuck.

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

public class BackgroundChange extends JFrame
{
    public BackgroundChange()
    {

    }

    public static void main(String[] args)
    {
        BackgroundChange frame = new BackgroundChange();
        frame.setTitle("Background Change");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setBackground(Color.WHITE);
    }
}

class BackgroundChangePanel extends BackgroundChange implements
MouseListener
{
    public BackgroundChangePanel()
    {
        addMouseListener(this);
    }

    public void mouseClicked(MouseEvent e)
    {

    }

    public void mouseEntered(MouseEvent e)
    {

    }

    public void mouseExited(MouseEvent e)
    {

    }

    public void mouseReleased(MouseEvent e)
    {
       
    }
   
    public void mousePressed(MouseEvent e)
    {
       
    }
}
Michael Rauscher - 28 Nov 2006 08:10 GMT
Rick schrieb:
> Here is the code I have.  Basically all I want to do is change the
> background of the frame to black when I click the mouse (mouseClicked)
> and change it back to white when I release the mouse (mouseReleased).
> I can't access frame directly to do frame.setBackground(Color.BLACK),
> and that is where I am stuck.

That's not the way it works. A frame is - hmm... a frame. What you see
on screen within a frame is not the frame but a container. Imagine the
frame as the window border.

A possible solution:

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

public class Test {
    private JPanel getContentPanel() {
        JPanel panel = new JPanel();
        panel.addMouseListener( new MouseAdapter() {
            public void mouseClicked( MouseEvent e ) {
                ((JComponent)e.getSource()).setBackground(Color.BLACK);
            }
        });
        panel.setPreferredSize( new Dimension(600,400) );
        return panel;
    }

    public void createAndShowGUI() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.setContentPane( getContentPanel() );
        frame.pack();
        frame.setVisible( true );
    }

    public static final void main( String args[] ) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                new Test().createAndShowGUI();
            }
        });
    }
}

Bye
Michael


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.