• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mouse events on transparent Swing components  RSS feed

 
N D Fisher
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have recently written a program in which I've got my JButtons set to transparent (using setOpaque(false)) - but I've noticed that the mouse events seem to work slightly differently when displaying buttons this way. It seems that with transparent components, events like MouseMoved, MouseEntered, etc, only fire when the mouse is placed directly over the *text* on the button - not when you move it just anywhere in the button area like on a normal opaque button.

This is quite annoying as you have to be a lot more accurate with your mouse movements to get the events to fire. I have noticed the same thing happens on JPanels too.

Is this a recognised problem, and if so, and is there a workaround?

Many thanks

Nick
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
works OK for me, java 1.6 winxp

post a demo program (frame/transparent button/listener only),
so that we can copy/paste/compile/run and see the behavior you've described.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works for me too.
Instead of setOpaque can you try it with setContentAreaFilled(false)?
 
N D Fisher
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm actually using both setContentAreaFilled(false) and setOpaque(false) for the panel and button. Here's the code:




If you comment out the line 'myPanel.setOpaque(false)' you will see what I mean. With this line commented out, the events respond as soon as you move the mouse anywhere in the button area. But with the line uncommented, you have to move the mouse to physically touch the text itself before the events start to respond. Seems like a quirk of the transparency. Any way to prevent this?!

I'm using Java 1.6.0_10 on Vista.

Nick
[ September 14, 2008: Message edited by: Rob Prime ]
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags. I've added them for you, and you can see how much better this is to read.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't run the code as-is (I don't use the beta version 10),
but if I comment out all the awtUtilities stuff, it works OK.

perhaps a search of sun's bug database might be worth doing.
 
N D Fisher
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help.

I know it works without the AWT stuff, but as soon as you put it in, that's when it starts acting weird. They probably still have some stuff to sort out with the beta version. I'll have a look round on the Sun web site, or maybe even post them the code so they can add it to their bug list.

Cheers


Nick
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!