Thursday, 17 May 2018

JavaFX: Button: create button

Button class provides below constructors to create button widget.

public Button()
public Button(String text)
public Button(String text, Node graphic)

Ex
Button button1 = new Button();
button1.setText("Search Button1");

Button button2 = new Button("Search Button2");

InputStream is = new FileInputStream(imageFilePath);
Image image = new Image(is);
Button button3 = new Button("Search Button3", new ImageView(image));

Find the below working applicaition.

ButtonApp.java
package com.sample.demos;

import static javafx.geometry.Pos.CENTER;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ButtonApp extends Application {
 private static String imageFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\search.png";

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException {
  Button button1 = new Button();
  button1.setText("Search Button1");

  Button button2 = new Button("Search Button2");

  InputStream is = new FileInputStream(imageFilePath);
  Image image = new Image(is);
  Button button3 = new Button("Search Button3", new ImageView(image));

  VBox vBox = new VBox(10, button1, button2, button3);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 400, 400);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Button Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(ButtonApp.class, args);
 }
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment