• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Correct this code for event handling.

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i've two classes called MyApplet extends Applet and MyComponent extends Component, and written separate mouselisteners for each.

i hav created an instance of MyComponent in MyApplet and now i want to handle the mouse events in MyComponent say, i've a OK button in MyComponent and after clicking on that only i should come back to MyApplet.

the problem i'm facing is could not able to handle mouse events in MyComponent but i could in MyApplet.

for more info plz, look into this code below, and suggest me in how to handle it properly.

thanx.

note: after creation of the instance, it returns immediately without listening to mouse events.

--------MyApplet.java-------------
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class MyApplet extends Applet implements MouseListener{

MyComponent comp;

public void init(){
addMouseListener(this);
}

public void paint(Graphics g){
comp = new MyComponent(g);
}

public void mousePressed(MouseEvent me){System.out.println("Mouse Pressed in MyApplet");}
public void mouseEntered(MouseEvent me){System.out.println("Mouse Entered in MyApplet");}
public void mouseReleased(MouseEvent me){System.out.println("Mouse Released in MyApplet");}
public void mouseExited(MouseEvent me){System.out.println("Mouse Exited in MyApplet");}
public void mouseClicked(MouseEvent me){System.out.println("Mouse Clicked in MyApplet");}

}


----------MyComponent.java-----------
import java.awt.*;
import java.awt.event.*;

public class MyComponent extends Component implements MouseListener{

MyComponent(Graphics g){
g.setColor(Color.RED);
g.fillRect(100, 100, 300, 200);
g.setColor(Color.WHITE);
g.drawString("This is My Component", 150, 150);

enableEvents(AWTEvent.MOUSE_EVENT_MASK);
addMouseListener(this);
MouseListener[] mls = (MouseListener[])(getListeners(MouseListener.class));
System.out.println("Registered Listeners are :: " + mls);
}

protected void processMouseEvent(MouseEvent e){
System.out.println("processMouseEvent called");
super.processMouseEvent(e);
}

public void mousePressed(MouseEvent me){System.out.println("Mouse Pressed in MyComponent");}
public void mouseEntered(MouseEvent me){System.out.println("Mouse Entered in MyComponent");}
public void mouseReleased(MouseEvent me){System.out.println("Mouse Released in MyComponent");}
public void mouseExited(MouseEvent me){System.out.println("Mouse Exited in MyComponent");}
public void mouseClicked(MouseEvent me){System.out.println("Mouse Clicked in MyComponent");}

}
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why would you want to extend the component class and then initialize it in your applet. I think its a better idea to just create an instance of whatever component you need inside your init() method (in the applet class). For example JButton some_button = new JButton(). (This is a swing component).

Consult the swing API for more info.
 
Akshay Kumar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fine. how to add a swing component to an applet when the total applet is displayed as single image, means all the controls been drawn on a image and finally this image is drawn to the applet using Graphics object.

pour comments on this.

thanx.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a demo in AWT. You mentioned swing which would be a little different.
 
Akshay Kumar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx. now, 'm clear in my stands.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic