Setup JavaFX Toggle Button - Java JavaFX

Java examples for JavaFX:Button

Description

Setup JavaFX Toggle Button

Demo Code



//package com.java2s;
import javafx.beans.value.ObservableBooleanValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Main {
  public static void SetupToggleButton(Button button, EventHandler<ActionEvent> event, ObservableBooleanValue property,
      Image graphic, String onCss, String offCss) {
    SetupButton(button, event, graphic, offCss);
    property.addListener((observable, oldValue, newValue) -> {
      button.getStylesheets().clear();//from  w w  w.  j a v a  2s  . co m
      String css = newValue ? onCss : offCss;
      button.getStylesheets().add(css);
    });
  }

  public static void SetupButton(Button button, EventHandler<ActionEvent> event, Image graphic, String css) {
    ImageView imageView = new ImageView(graphic);
    button.setGraphic(imageView);
    button.setOnAction(event);
    button.getStylesheets().add(css);
  }
}

Related Tutorials