Can anyone please suggest me a method to create a on/off switch button like as the image shown below, using JavaFX.
I have tried to find a method but couldn't get it.
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.
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.
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.
In the same manner, we disable the button using the statement, button. setDisable(true);
Update
ControlsFX has a ToggleSwitch control.
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 :
There are few other ToggleSwitches available (which I have never tried) :
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With