Re: 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 21:36:19 +0200

Le 29/10/2004 à 18:17:43, Andrew Thompson <SeeMySites@www.invalid> a
écrit:

> Mixing light and heavywieght components can be problematic.
> Until you have a lot of experience with both and are familiar
> with why, you should avoid doing it.
> <http://www.physci.org/guifaq.jsp#5.1>

thx, i switch to JComponent.

> [...]

Sorry for not being precise.

1/ I don't know why it didn't work, but know it does:

// the HomePanel
public class HomePanel extends javax.swing.JPanel {
    
    public HomePanel() {
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent evt) {
                HomePanel.this.mousePressed(evt);
            }
        });
    }
    
    private void mousePressed(MouseEvent evt) {
        System.err.println("DEBUG: HomePanel.mousePressed");

        add(New HomeComponent());
        repaint();
    }
}

// the HomeComponent
public class HomeComponent extends javax.swing.JComponent {

    public HomeComponent() {
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent evt) {
                HomeComponent.this.mousePressed(evt);
            }
        });
    }

    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 by Netbeans code
       getContentPane().add(homePanel);
       ...
}

Output:
DEBUG: MainFrame.main
DEBUG: HomePanel.mousePressed <= click on the panel
DEBUG: HomeComponent.paint
DEBUG: HomeComponent.mousePressed <= click on the component

the click on the component is not handled by the panel => success :D

2/ Adapter(s) avoid to define all the methods of Listener(s)
interface, and since Adapter are class and Java cannot multi-herit
everybody use inlive inner class definition of adapter.

Thank you and google ;)

-- 
Nomak


Relevant Pages

  • CSharp Coding Standards
    ... Prefix interface names with I: ... Avoid using Hungarian notation for public or protected members. ... public class LinkedList ... public class PublicKeys ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: CSharp Coding Standards
    ... Prefix interface names with I: ... Avoid using Hungarian notation for public or protected members. ... public class LinkedList ... public class PublicKeys ...
    (microsoft.public.dotnet.languages.csharp)
  • 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: dynamic tool tip text
    ... public class CustomTextField extends JTextField { ... public String getToolTipText{ ... public void mouseEntered{ ...
    (comp.lang.java.programmer)
  • 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)