• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling events

 
Luis De Bello
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I wanted to ask you your opinion about how to handle the iteration between two different events modifying the style for the same component

I have one label and this has to change its style when the mouse is over the label and recover the default state when the mouse exit the label.

Normal state:
Label with text size 10px

Mouse Entered:
Label with text size 12px
Dash Border green color
Label size 100px

When the mouse exit the component should recover the default state

I have to do this kind of thing for several components and the properties that I need to change are different so I would like to ask you if you have any suggestion on how to handle this kind of change in an elegant way.

Maybe there is a pattern to simplify these cases.

Thanks in advance for your help.

Best regards,
Luis
 
Campbell Ritchie
Sheriff
Posts: 51344
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

At first I thought, “Events: use a λ”, but you cannot use a λ for MouseListener because MouseListener is not a functional interface.

I don't think your problem is very difficult. Start by adding a MouseListener to the Label. Maybe a concrete class would be a good idea, then you can use it for several labelsThat is one way to do it. There are doubtless lots of other ways. As you can see my code is not complete.
 
Campbell Ritchie
Sheriff
Posts: 51344
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware: changing the size of text or the size of the label when the mouse enters it will produce a very peculiar appearance.
 
Luis De Bello
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell

Thanks for your comments, you are right I can use one MouseAdapter my problem is that I am working in JavaFX, I have just realised that there was a specific forum about JavaFX.

Do you know how to move to question for JavaFX forum?

In JavaFX the events expect for one EventHandler which has one Event, so if I need to handle the same component from several events I need to create handlers and pass the event in the constructor also if I modified some specified properties at some point I need to revert the same properties to the original values, so I was looking for a good way of doing this. That is the reason why I asked about some pattern or some like that because maybe there is something already built to handle this case using one pattern and some reflection.

Regards,
Luis
 
Campbell Ritchie
Sheriff
Posts: 51344
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know about FX, I am afraid. You can probably use the same pattern but a different event handler.

Shall move you to the FX forum.
 
Darryl Burke
Bartender
Posts: 5149
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried using CSS and the hover pseudo-class?

JavaFX CSS Reference Guide
 
Luis De Bello
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl,

I cannot use the CSS with the pseudo class hover because I need to do some changes like increase the width size in some cases and in other keep the same size.

Normal State:
Case 1:
Label: "Hello World"
In this case I need to use a dash border but if the size is less than 150px I need to increase the size to border a bigger area

Case 2:
Label "Hello World with long text which is bigger than 150px"
In this case the border is only around this phrase.

As far as I know this cannot be done using CSS.


Regards,
Luis
 
John Damien Smith
Ranch Hand
Posts: 296
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a listener on the hoverProperty of the node and perform your work there.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic