B4J Library jToggleSwitch V1.0.0

ThRuST

Well-Known Member
Licensed User
Longtime User
Here's is a similar toggleswitch made in native Java. It might be of interest to people visiting this thread. Have a look here
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Allready did. We posted at the same time funny, that's a good omen haha
 

ThRuST

Well-Known Member
Licensed User
Longtime User
@DonManfred This might help you alot. The Native Java source code

Go here for the source

I also post the code bwlow, for lazy people like myself


B4X:
package com.almasb.ios;

import javafx.animation.FillTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class IOSApp extends Application {

    private Parent createContent() {
        Pane root = new Pane();
        root.setPrefSize(300, 300);

        Rectangle bg = new Rectangle(300, 300);

        ToggleSwitch toggle = new ToggleSwitch();
        toggle.setTranslateX(100);
        toggle.setTranslateY(100);

        Text text = new Text();
        text.setFont(Font.font(18));
        text.setFill(Color.WHITE);
        text.setTranslateX(100);
        text.setTranslateY(200);
        text.textProperty().bind(Bindings.when(toggle.switchedOnProperty()).then("ON").otherwise("OFF"));

        root.getChildren().addAll(toggle, text);
        return root;
    }

    private static class ToggleSwitch extends Parent {

        private BooleanProperty switchedOn = new SimpleBooleanProperty(false);

        private TranslateTransition translateAnimation = new TranslateTransition(Duration.seconds(0.25));
        private FillTransition fillAnimation = new FillTransition(Duration.seconds(0.25));

        private ParallelTransition animation = new ParallelTransition(translateAnimation, fillAnimation);

        public BooleanProperty switchedOnProperty() {
            return switchedOn;
        }

        public ToggleSwitch() {
            Rectangle background = new Rectangle(100, 50);
            background.setArcWidth(50);
            background.setArcHeight(50);
            background.setFill(Color.WHITE);
            background.setStroke(Color.LIGHTGRAY);

            Circle trigger = new Circle(25);
            trigger.setCenterX(25);
            trigger.setCenterY(25);
            trigger.setFill(Color.WHITE);
            trigger.setStroke(Color.LIGHTGRAY);

            DropShadow shadow = new DropShadow();
            shadow.setRadius(2);
            trigger.setEffect(shadow);

            translateAnimation.setNode(trigger);
            fillAnimation.setShape(background);

            getChildren().addAll(background, trigger);

            switchedOn.addListener((obs, oldState, newState) -> {
                boolean isOn = newState.booleanValue();
                translateAnimation.setToX(isOn ? 100 - 50 : 0);
                fillAnimation.setFromValue(isOn ? Color.WHITE : Color.LIGHTGREEN);
                fillAnimation.setToValue(isOn ? Color.LIGHTGREEN : Color.WHITE);

                animation.play();
            });

            setOnMouseClicked(event -> {
                switchedOn.set(!switchedOn.get());
            });
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setScene(new Scene(createContent()));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
 

ThRuST

Well-Known Member
Licensed User
Longtime User
@DonManfred So how do you like the one I found? was it similar in code to the one you did?
please save my arse in this post go here
 

stevel05

Expert
Licensed User
Longtime User
OK in an attempt to put this one to bed, I took a peek at Don Manfreds library, after he gave permission. It wraps the controlsFX Toggleswitch, which is not available in the Java 9 Version.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…