aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Correct this code for event handling. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Correct this code for event handling." Watch "Correct this code for event handling." New topic
Author

Correct this code for event handling.

Akshay Kumar
Ranch Hand

Joined: Feb 24, 2004
Posts: 37
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");}

}
Maysam Sorkhabi
Greenhorn

Joined: Jul 13, 2004
Posts: 23
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

Joined: Feb 24, 2004
Posts: 37
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.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Here's a demo in AWT. You mentioned swing which would be a little different.
Akshay Kumar
Ranch Hand

Joined: Feb 24, 2004
Posts: 37
thanx. now, 'm clear in my stands.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Correct this code for event handling.
 
Similar Threads
AWT
JComponent problem ?
Paint Drag Preview
TextField Population of Mouse Events?
How to make popup invisible