How to make java dispatch mouse events to my component

From: Nomak (no.email_at_invalid.domain.fr)
Date: 10/29/04


Date: Fri, 29 Oct 2004 17:02:20 +0200

Hello,

I have a home made panel (which extend JPanel), and a home made
graphical component (which extend java.awt.Component).

What i want to do is:
- when i click on the HomePanel, it's add a HomeComponent to it.
- when i click on a HomeComponent in the HomePanel, it change it's
color.

Right now, my code is:

// the HomePanel
public class HomePanel extends JPanel {
}

// the HomeComponent
public class HomeComponent extends Component
                 implements MouseListener {
    public void paint(Graphics g) {
        ...
    }

    public void mousePressed(MouseEvent e) {
        System.err.println("DEBUG: HomeComponent.mousePressed");
    }

    ...
}

// and the MainFrame:
public class MainFrame extends javax.swing.JFrame {
       ...
        // generated code
        homePanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                homePanelMousePressed(evt);
            }
        });
       getContentPane().add(homePanel);
       ...

    private void homePanelMousePressed(java.awt.event.MouseEvent evt){
        System.err.println("DEBUG: MainFrame.homePanelMousePressed");
        homePanel.add(new HomeComponent());
        homePanel.repaint();
    }
}

1/ Why does it don't work?
2/ What is the interest of MouseAdaptater(s) over MouseListener(s)?

Thanks for your help.

-- 
Nomak


Relevant Pages

  • program challenge
    ... public void setName{ ... the salaried class that extends Employee ... public class Salaried extends Employee ... // Programmer.java: the programmer class that extends from Hourly ...
    (comp.lang.java.help)
  • Re: Exceptions
    ... >> public abstract class BasePermissionException extends ... >> public abstract class BaseRulesException extends BaseRuntimeException ... >> public class PermissionException extends BasePermissionException ... > public class PermissionException extends RuntimeException ...
    (comp.lang.java.help)
  • Re: is it ok to return null?
    ... A refactoring to be aware of also is the "Introduce Null Object". ... The refactoring includes creating a Null Object that extends the original, ... public class TestEmployee extends TestCase ...
    (comp.lang.java.help)
  • A good way to approach this?
    ... public class AutoFillTextArea extends JTextArea implements CaretListener { ... caret. ...
    (comp.lang.java.gui)
  • Re: ADTs vs. interfaces
    ... >>mixture of abstract methods and concrete methods and your variables ... >>With an interface, it's a little different. ... you can't use extends from an interface. ... > public class BigBooks implements Books { ...
    (comp.lang.java.help)