Label mouse in and out event : Label « JavaFX « Java






Label mouse in and out event

 
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();

    final Label label1 = new Label("Search long long long long long long long long long ");
    label1.setOnMouseEntered(new EventHandler<MouseEvent>() {
      @Override
      public void handle(MouseEvent e) {
        label1.setScaleX(1.5);
        label1.setScaleY(1.5);
      }
    });

    label1.setOnMouseExited(new EventHandler<MouseEvent>() {
      @Override
      public void handle(MouseEvent e) {
        label1.setScaleX(1);
        label1.setScaleY(1);
      }
    });
    
    hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

    stage.setScene(scene);
    stage.show();
  }
}

   
  








Related examples in the same category

1.Using Label to display Text
2.Set new value to Label
3.Set Font for Label
4.Set Label Text color
5.Using Rotate to create vertical label
6.Move a Label by using setTranslateY
7.Wrap a Label
8.Scale a Label
9.Adding Image to Label
10.Change Label text in Button click event