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 / October 2004

Tip: Looking for answers? Try searching our database.

How to make java dispatch mouse events to my component

Thread view: 
Nomak - 29 Oct 2004 16:02 GMT
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.

Signature

Nomak

Andrew Thompson - 29 Oct 2004 17:17 GMT
> I have a home made panel (which extend JPanel), and a home made
> graphical component (which extend java.awt.Component).

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>

> 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:
...
> 1/ Why does it don't work?

Several ways to answer this..
a) It must compile first.
b) Define 'work'
c) Try flogging it wiyh a whip.  It may just be lazy.   ;-)

Now, here is some code that copy/pasted into a file named
'MainFrame.java' will compile and run..

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// the HomePanel
class HomePanel extends JPanel {
}

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

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

   public void mouseReleased(MouseEvent e) {
       System.err.println("" + e);
   }
   public void mouseClicked(MouseEvent e) {
       System.err.println("" + e);
   }
   public void mouseEntered(MouseEvent e) {
       System.err.println("" + e);
   }
   public void mouseExited(MouseEvent e) {
       System.err.println("" + e);
   }
   // ...
}

// and the MainFrame:
public class MainFrame extends javax.swing.JFrame {

   HomePanel homePanel = new HomePanel();

   MainFrame() {
      // ...

       // generated code
       //  What does that mean?  generated by what?
       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();
   }

   public static void main(String[] args) {
       MainFrame mainFrame = new MainFrame();
       mainFrame.setSize(100,100);
       mainFrame.setVisible(true);
   }
}
</sscce>

It 'works' for certain values of 'works'.

> 2/ What is the interest of MouseAdaptater(s) over MouseListener(s)?

Who's interest?  The code uses a MouseListener.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Nomak - 29 Oct 2004 20:36 GMT
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 ;)

Signature

Nomak

Andrew Thompson - 29 Oct 2004 20:46 GMT
> the click on the component is not handled by the panel => success :D

Excellent work, glad you sorted it.
And thanks for getting back to us.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane



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.