java


how to get mouse pointer component in swing application


my swing application one panel have 6 button. when cursor goes on the button i want to change default cursor to hand cursor and cursor is exit then it want to change default cursor. Now i am doing this thing using below code.
private void btnRegisterReceiptMouseEntered(java.awt.event.MouseEvent evt) {
btnRegisterReceipt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
private void btnRegisterReceiptMouseExited(java.awt.event.MouseEvent evt) {
btnRegisterReceipt.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
Now i want to write this code to each and every button.
But I want to write common method to do this one. I already try to use MouseListener do this thing, but I can not get which is the mouse point component.
I don't know it is possible or not. if it is possible please anyone tell me how to do this things.
private void changeCursor() {
addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered( MouseEvent e ) {
/*if ( mouse Entered compornent is button ) {
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
button.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}*/
}
});
}
Write a generic MouseListener (as an annonymouse class):
MouseListener ml = new MouseAdapter()
{
#Override
public void mouseEntered(MouseEvent e)
{
e.getComponent( setCursor(...) );
}
#Override
public void mouseExited(MouseEvent e)
{
e.getComponent( setCursor(...) );
}
};
Then you can just add the MouseListener to any component you want with:
btnRegisterReceipt.addMouseListener( ml );
anotherButton.addMouseListener( ml );
You can also make this as a reusable class:
public MousePointerListener extends MouseAdapter
{
#Override
public void mouseEntered(MouseEvent e)
{
e.getComponent( setCursor(...) );
}
#Override
public void mouseExited(MouseEvent e)
{
e.getComponent( setCursor(...) );
}
}
Then you use:
MouseListener ml = new MousePointerListener();
btnRegisterReceipt.addMouseListener( ml );
anotherButton.addMouseListener( ml );
The key point in both examples is that you can get the source of the event from the MouseEvent, which allows you to write generic code. You should look at this approach for all you listeners, instead of letting your IDE generate the listener code.

Related Links

How to add an integer in a string, java and limit user input
Can floating point inaccuracy cause Math.random() or new Random().nextFloat() return a value <0 or >=1 in practical?
Do-While loop issue, making the prompt run again -JAVA
Android Bitmap parcel Unmarshalling error while broadcast it
How to force Commons HTTPClient 3.1 to use TLS 1.2 only for HTTPS?
Java webapps - why is index.html displayed when visiting a webapps root directory?
Obtaining memory statistics from all JVMs without using JMX
I have an Array which i Want to Strip and Convert to another array
In Java, is it safe to lock and synchronize on the same object? [duplicate]
Android Eclipse 'Source not found' at execute
Implementation of previous() and hasPrevious() method in a doubly linked lists
How to get all videos from youtube playlist with youtubeApi for java?
ACL settings with S3 REST API using Java
Tomcat Unable to compile class for JSP in Servlet 3.0
How to send the notifications through my message after reminder task finished in back end
Advice and help if there is shorter way to write code for this task

Categories

HOME
sendgrid
testing
vim
client
wso2
angular-material
getelementsbytagname
rsync
icloud
windows-server
datastax-java-driver
add
apache-cayenne
decomposition
caml
serverless-framework
openrefine
cx-freeze
orleans
mmap
dxf
one-to-many
django-storage
selectedindexchanged
chromebook
microsoft-chart-controls
calibre
ghost4j
catalog
jmonkeyengine
swisscomdev
vapor
sqlite2
awt
sas-visual-analytics
hp-ux
sencha-touch-2.3
user-accounts
appcompat
promela
paxos
typescript1.8
yii2-extension
lift-json
nsarray
account-kit
objective-c-swift-bridge
time-and-attendance
instant
xml-attribute
react-native-listview
apachebench
pagerank
ipconfig
two-factor-authentication
associative-array
kendonumerictextbox
ruby-2.2
content-length
nsight
skos
notify
e10s
kineticjs
cdt
relocation
rdtsc
wordpress-theme-customize
xojo
google-reader
tld
dml
phpthumb
wp-query
viadeo
uv-mapping
gridfs
ftps
html4
eventual-consistency
html-editor
drools-planner
anonymous-methods
pvrtc
onsubmit
fluent-interface
coda-slider
dentrix
rescale
bespin
mtj
temporal-database
post-build
mdac
lzh

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App