java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow How to program a panel that handles its own mouse events
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to program a panel that handles its own mouse events E-mail
User Rating: / 7
PoorBest 
JPanel

This Java Swing tip illustrates a method of programming a panel that handles its own mouse events. A self-contained JPanel class that handles its own mouse events.

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

public class SelfContainedPanel extends JPanel {
   
   private int x1, y1, x2, y2;

   public SelfContainedPanel()
   {
      addMouseListener(
         new MouseAdapter() {
            public void mousePressedMouseEvent e )
            {
               x1 = e.getX();
               y1 = e.getY();
            }

            public void mouseReleasedMouseEvent e )
            {
               x2 = e.getX();
               y2 = e.getY();
               repaint();
            }
         }
      );

      addMouseMotionListener(
         new MouseMotionAdapter() {
            public void mouseDraggedMouseEvent e )
            {
               x2 = e.getX();
               y2 = e.getY();
               repaint();
            }
         }
      );
   }

   public Dimension getPreferredSize()
   {
      return new Dimension150100 );
   }

   public void paintComponentGraphics g )
   {
      super.paintComponent);

      g.drawOvalMath.minx1, x2 ), Math.miny1, y2 ),
                  Math.absx1 - x2 ), Math.absy1 - y2 ) );
   }
}

// Creating a self-contained subclass of JPanel 
// that processes its own mouse events.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SelfContainedPanelTest extends JFrame {
   private SelfContainedPanel myPanel;

   public SelfContainedPanelTest()
   {
      myPanel = new SelfContainedPanel();   
      myPanel.setBackgroundColor.yellow );

      Container c = getContentPane();
      c.setLayoutnew FlowLayout() );
      c.addmyPanel );

      addMouseMotionListener(
         new MouseMotionListener() {
            public void mouseDraggedMouseEvent e )
            {
               setTitle"Dragging: x=" + e.getX() +
                         "; y=" + e.getY() );
            }

            public void mouseMovedMouseEvent e )
            {
               setTitle"Moving: x=" + e.getX() +
                         "; y=" + e.getY() );
            }
         }
      );

      setSize300200 );
      show();
   }

   public static void mainString args[] )
   {
      SelfContainedPanelTest app =
         new SelfContainedPanelTest();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosingWindowEvent e )
            {
               System.exit);
            }
         }
      );
   }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.