Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating sliding on/off Switch button in javaFX

Tags:

javafx

Can anyone please suggest me a method to create a on/off switch button like as the image shown below, using JavaFX.

enter image description here

I have tried to find a method but couldn't get it.

like image 826
vineeth Avatar asked Jun 02 '15 10:06

vineeth


People also ask

How do I use toggle button in JavaFX?

Create a ToggleGroup and new Toggle Buttons named”Male” and “Female“. Set the Toggle Group (in a group only one button can be selected at a time) using setToggleGroup() method. By default Male button is selected. Create the scene and set scene to the stage using setScene() method.

How do I program a button in JavaFX?

You can create a Button by instantiating the javafx. scene. control. Button class of this package and, you can set text to the button using the setText() method.

What is toggle group in JavaFX?

public class ToggleGroup extends Object. A class which contains a reference to all Toggles whose selected variables should be managed such that only a single Toggle within the ToggleGroup may be selected at any one time.

Which method can you use to disable a button in JavaFX?

In the same manner, we disable the button using the statement, button. setDisable(true);


2 Answers

Update

ControlsFX has a ToggleSwitch control.

ToggleSwitch


I had created a simple ToggleSwitch using JavaFX. It is a very basic model, but will help you to understand how you can go along with a more complex control of your own.

Here is a gist to my code.

In case you are wondering, how it looks :

enter image description here

There are few other ToggleSwitches available (which I have never tried) :

  1. JFXtras
  2. JavaFX Metro Theme ToggleSwitch
like image 148
ItachiUchiha Avatar answered Nov 01 '22 18:11

ItachiUchiha


4 years late to the party, but maybe more people stumble upon this question. I modified ItachiUchiha's code snippet a little. It's just another design with the circle overlapping the bar.

SwitchButton

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;

public class SwitchButton extends StackPane {
    private final Rectangle back = new Rectangle(30, 10, Color.RED);
    private final Button button = new Button();
    private String buttonStyleOff = "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 0.2, 0.0, 0.0, 2); -fx-background-color: WHITE;";
    private String buttonStyleOn = "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 0.2, 0.0, 0.0, 2); -fx-background-color: #00893d;";
    private boolean state;

    private void init() {
        getChildren().addAll(back, button);
        setMinSize(30, 15);
        back.maxWidth(30);
        back.minWidth(30);
        back.maxHeight(10);
        back.minHeight(10);
        back.setArcHeight(back.getHeight());
        back.setArcWidth(back.getHeight());
        back.setFill(Color.valueOf("#ced5da"));
        Double r = 2.0;
        button.setShape(new Circle(r));
        setAlignment(button, Pos.CENTER_LEFT);
        button.setMaxSize(15, 15);
        button.setMinSize(15, 15);
        button.setStyle(buttonStyleOff);
    }

    public SwitchButton() {
        init();
        EventHandler<Event> click = new EventHandler<Event>() {
            @Override
            public void handle(Event e) {
                if (state) {
                    button.setStyle(buttonStyleOff);
                    back.setFill(Color.valueOf("#ced5da"));
                    setAlignment(button, Pos.CENTER_LEFT);
                    state = false;
                } else {
                    button.setStyle(buttonStyleOn);
                    back.setFill(Color.valueOf("#80C49E"));
                    setAlignment(button, Pos.CENTER_RIGHT);
                    state = true;
                }
            }
        };

        button.setFocusTraversable(false);
        setOnMouseClicked(click);
        button.setOnMouseClicked(click);
    }
}
like image 45
Meiswjn Avatar answered Nov 01 '22 20:11

Meiswjn