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

return already casted object to the concrete class type
One-time Java Configuration file on a desktop app
apache HttpGet with params
How to replace the global exception handler in JUnit 4.12 with one printing “root cause first” stack trace?
Why is my console getting spammed with warnings
Jamod slave example
Application Insights support multi environment for java
Spring MVC helloworld - Package name cannot start with java?
Why isn't the recursion code calling all the cases?
org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference when tomcat server run in eclipse
Can't Run Activator Template projects Activator UI Failed to load SBT configuration
Can someone help me fix “Exception in thread ”main“ java.lang.ArrayIndexOutOfBoundsException: 7” error?
display database table in java
How to authenticate redis cache in imcache?
Admob ads giving error code 2 (not displaying)
Slow double buffering method

Categories

HOME
osgi
netsuite
image-processing
appx
rsyslog
amazon-ecs
c#-2.0
win32gui
baqend
directx
cloudkit
offline
rascal
orchardcms
try-catch
seaborn
tostring
sms-gateway
visual-composer
immutable.js
physics-engine
realex-payments-api
devops
crystal-reports-2010
vlsi
facet
linkerd
unboundid
nat
iframe-resizer
key-value-observing
lxd
windows-dev-center
openoffice.org
ghost4j
fusionpbx
starteam
service-discovery
code-search-engine
sas-visual-analytics
total-commander
s
executenonquery
xml-documentation
apache-fop
dds
typescript1.8
capacity
color-profile
titanium-android
root-framework
abcpdf9
py2app
lttng
hill-climbing
rhino-servicebus
pyke
maven-tomcat-plugin
mutation-observers
spidermonkey
riak-cs
myo
jsapi
system.reflection
phpthumb
yorick
xceed-datagrid
unc
libstdc++
propertyeditor
tridion-worldserver
jspinclude
code-cleanup
hirefire
sql-server-profiler
sudzc
suppress
getresponsestream
jquery-ui-droppable
rescale
webkit.net
javap
uimenucontroller
boost-smart-ptr
phonon
project-hosting

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App