6 replies
by senbe
by bbuscher
The mouse pointer, or cursor is treated as a part of a java Component class. Component is in the java.awt package, and the superclass of most of the invisible components. So, the change the cursor you use the setCursor() method of the component class. See the java doc online at http://www.javasoft.com/products/jdk/1.2/docs/api/java/awt/Cursor.html
Code as follows:
java.awt.Cursor MyCursor = new Cursor(HAND_CURSOR);
Mycomponent.setCursor(Mycursor);
//now your cursor is a hand :-)
Note: this only has effect when the cursor is over the component, so choose which component you invoke this on accordingly.
by senbe
by imamuddin
There are several implementation to change the mouse pointer in Java Applet.
1. You can change the mouse pointer directly by calling method setCursor.
2. You can create your own classes, implement the MouseListener and change the mouse pointerfrom the selected method.
Here is the sample
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MousePointerTest extends Applet {
private myComponent myComp;
private Cursor curAppletArea; public void init() {
setLayout(new FlowLayout());
myComp=new myComponent();
add (myComp);
// change mouse pointer over here
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public void paint(Graphics g) {
g.drawString ("Mouse Pointer Demo",10,10);
}
}
class myComponent extends Canvas implements MouseListener{
private Cursor curInArea;
public myComponent() {
super();
curInArea=new Cursor(Cursor.HAND_CURSOR);
setBounds(5,10,100,100);
addMouseListener(this);
}
public void paint (Graphics g) {
g.drawRect (0,0,getBounds().width-1,getBounds().height-1);
}
// or you can change mouse pointer with the selected event
public void mouseEntered(MouseEvent e) {
this.setCursor (curInArea);
}
public void mouseClicked(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
by senbe
by senbe
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending