Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

want to forward MouseEvents from child to parent component

843806Sep 12 2007 — edited Sep 13 2007
Could someone please help me with this little sample app. I'm trying to figure out how to forward MouseEvents from a child component (JTextArea) to the parent component (the contentPane). The contentpane is simply noting xy coordinates. When I move my cursor over the child component, it gets the focus and thinks I want to provide input. But I don't! I want this app to continue providing me with xy coordinates regardless of what child component the cursor enters. I need to forward coordinates from the JTextArea to the contentPane somehow. I think I'm on the right track but need a little help.

Sample app code follows.

Thanks,

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


public class ReadComponent
{
  private boolean SELECT_MODE = true;
  private JLabel statusBar;
  private TextComponent tc = null;

  public static void main(String[] args)
  {
    ReadComponent rc = new ReadComponent();    
  }

  public ReadComponent()
  {
    //Create and set up the window.
    JFrame frame = new JFrame("ReadComponent");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container c = frame.getContentPane();
    statusBar = new JLabel();
    tc = new TextComponent(2,4,c);
    c.add(tc, BorderLayout.NORTH);
    c.add(statusBar, BorderLayout.SOUTH);
    MouseTracker mt = new MouseTracker();
    c.addMouseListener(mt);
    c.addMouseMotionListener(mt);
    frame.setSize(300, 200);
    frame.setVisible(true);    
  }  

  class MouseTracker implements MouseListener, MouseMotionListener
  {
    public void mouseClicked(MouseEvent e)
    {
      statusBar.setText("Clicked at [" + e.getX() + ", " + e.getY() + "]");
    }
    public void mouseEntered(MouseEvent e)
    {
      statusBar.setText("Entered at [" + e.getX() + ", " + e.getY() + "]");
    }
    public void mouseExited(MouseEvent e)
    {
      statusBar.setText("Exited at [" + e.getX() + ", " + e.getY() + "]");
    }
    public void mousePressed(MouseEvent e)
    {
      statusBar.setText("Pressed at [" + e.getX() + ", " + e.getY() + "]");
    }
    public void mouseReleased(MouseEvent e)
    {
      statusBar.setText("Released at [" + e.getX() + ", " + e.getY() + "]");
    }
    public void mouseDragged(MouseEvent e)
    {
      statusBar.setText("Dragged at [" + e.getX() + ", " + e.getY() + "]");
    }
    public void mouseMoved(MouseEvent e)
    {
      statusBar.setText("Moved at [" + e.getX() + ", " + e.getY() + "]");
    }
  }

  class TextComponent extends JTextArea
  {
    private Component parent;

    public TextComponent(int rows, int columns, Component parent)
    {
      super(rows,columns);
      this.parent = parent;
    }

    public void processMouseEvent(MouseEvent evt)
    {
      if(SELECT_MODE)
      {
        parent.dispatchEvent(evt); //<<This isn't doing what I had hoped
        System.out.println("parent.getName(): " + parent.getName());
      }
      else
      {
        super.processMouseEvent(evt);
      }
    }
  }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 11 2007
Added on Sep 12 2007
4 comments
1,526 views