Develop GUI Applications using Java programming language-Xplore IT Corp

Hi folks,

Here I have made a Customized toggle button using Javafx application which is used to build Rich Internet Applications.

To develop GUI Applications using Java programming language, the programmers rely on libraries such as AWT and Swing. After the advent of JavaFX, these Java programmers can now develop GUI applications effectively with rich content.

Below I have attached a coding for customized togglebutton.

 

 

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 JavaFXApplication1 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.BLACK);

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.RED);

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.RED: Color.LIGHTGREEN);

fillAnimation.setToValue(isOn ? Color.LIGHTGREEN : Color.RED);

 

animation.play();

});

 

setOnMouseClicked(event -> {

switchedOn.set(!switchedOn.get());

});

}

}

 

@Override

public void start(Stage primaryStage) {

primaryStage.setScene(new Scene(createContent()));

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

}

}

 

Please do comment for more queries.

Thanks in Advance!!!

Proud to be a Xplorer!!!

0 responses on "Develop GUI Applications using Java programming language-Xplore IT Corp"

Leave a Message

Your email address will not be published. Required fields are marked *

© Xplore IT Corp 2017. All rights reserved. Designed by ProPlus Logics
Xplore WhatsApp Chat
Send via WhatsApp
X