This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Java Swing MouseListener work once / repaint

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tryed to put jlabel underlined when mouse is over and not underline when mouse exit label but when I exit mouse the label continue underlined...below is the code I hv


Staff note (Paul Clapham):

I moved this post to the Swing forum.

 
Saloon Keeper
Posts: 11018
243
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Fred!

It's hard to read your code because it's not formatted properly. Use [code] tags to retain indentation and to use a monospace font.

I also see that you have a lot of fields and initialization code that is not related to your issue. Please trim it.
 
Bartender
Posts: 719
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In all of your listeners, why are you destroying the label and creating a new one? You should just change the text of the existing label.

Also, setting absolute bounds on components is a bad idea. Use layouts instead.
 
Marshal
Posts: 67036
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly don't alter old posts other than as requested. We need to see the original code; if there are any changes please post them in a new post. I have reverted the changes. Unfortunately that means you will have to add the code tags again, I am afraid.
 
Stephan van Hulst
Saloon Keeper
Posts: 11018
243
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt pointed out the source of your problem: you added the listener to your initial label, but when you enter the label you remove it and create a new one. The new label doesn't have any listeners associated with it.

In general you want to avoid adding and removing components after you've started displaying the container to the user. As Fred said, modify the existing components instead.

Furthermore, do not let the class that models your GUI implement any listeners. It's better to add listeners to components by letting anonymous classes extend adapters:

As you can see, this way you don't need to check the source of the event. I also removed a lot of code duplication by making separate methods for applying and removing the hover appearance.
 
Fred Granger
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Fred Kleinschmidt pointed out the source of your problem: you added the listener to your initial label, but when you enter the label you remove it and create a new one. The new label doesn't have any listeners associated with it.

In general you want to avoid adding and removing components after you've started displaying the container to the user. As Fred said, modify the existing components instead.

Furthermore, do not let the class that models your GUI implement any listeners. It's better to add listeners to components by letting anonymous classes extend adapters:

As you can see, this way you don't need to check the source of the event. I also removed a lot of code duplication by making separate methods for applying and removing the hover appearance.



Thanks! Works fine.
 
What are you doing? You are supposed to be reading this tiny ad!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!