Wednesday, 21 October 2015

Tutorial On Button In Java Fx

In this article I have tried to show you how to use button in javafx application button in javafx can be used in three different modes.
  1. NORMAL:Creates a simple push button.
  2. DEFAULT:Creates a simple button that can be operated by enter key.
  3. CANCEL:Creates a button that can be operated by escape button.
In this program we have shown you how to create default button.setLayoutX,setLayoutX is used to position of button in screen.

CODING:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;


public class mainfx extends Application 
{

@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("FX Button");
Button btn = new Button("Press To Exit");
btn.setMinWidth(100);
btn.setLayoutX(100);
btn.setLayoutY(100);

btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(1);
}
});

g.getChildren().add(btn);
Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}


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

}

APPLYING MOUSE EVENT IN BUTTON:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class mainfx extends Application {

Button btn = new Button();
@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("Button Event In JavaFx");
btn = new Button("Enter Mouse To Rotate");
btn.setMinWidth(100);
btn.setLayoutX(100);
btn.setLayoutY(100);

btn.addEventHandler(MouseEvent.MOUSE_ENTERED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
btn.getTransforms().add(new Rotate(45));
}
});    

g.getChildren().add(btn);
Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}


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

}









No comments:

Post a Comment