Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the cursor in a JApplet

Status
Not open for further replies.

louisgnarf

Programmer
Jul 11, 2002
14
US
What's the easiest way to go about changing what the cursor looks like as it passes over the area of a JApplet?
 
The cursor can have many types, look in the java.awt.Cursor package..
You can set the cursor of the components like this. I have added an example that changes the cursor when entering the mouse over a JButton. Add the code below where appropriate in your applet code...I have left out init method etc..

You must implement the MouseListener interface and handle the cursor change there..add the relevant components as listeners to mouse events...

hope its usefull


import java.awt.Cursor;


public class ChangeCursor
extends javax.swing.JApplet
implements java.awt.event.MouseListener
{
Cursor dc;
Cursor hc;
//add your components
public ChangeCursor()
{
dc = new Cursor(Cursor.DEFAULT_CURSOR);

JButton button = new JButton("Push me mate..");

JPanel panel = new JPanel();

panel.setBackground(java.awt.Color.gray);
panel.add(button);
getContentPane().setLayout(new BorderLayout());
getContentPane().add( panel,BorderLayout.CENTER);

//Add components as listeners to mouse events
button.addMouseListener(this);
panel.addMouseListener(this);
this.addMouseListener(this);


// Method used for MouseListener interface

public void mouseExited(java.awt.event.MouseEvent p0)
{

}
public void mousePressed(java.awt.event.MouseEvent p0){

}

public void mouseReleased(java.awt.event.MouseEvent p0){
}

public void mouseEntered (java.awt.event.MouseEvent p0){

if (p0.getComponent() instanceof JButton)
{
System.out.println("ITS a button");
hc = new Cursor(Cursor.HAND_CURSOR);
this.setCursor(hc);
}
else {
this.setCursor(dc);

}

}


}


Jimhimselv

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top