JavaFX Button Example

 JavaFX Button Example :

JavaFX Button Example how to Set Graphics on buttons:

Read more about how to apply css on Button at JavaFX Button CSS

JavaFX Button Example code:


package javafxtuts;

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.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author wordpress.JavaFXtuts.com
*/

public class JavaFXTuts extends Application {

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

@Override
public void start(Stage mystage) {
Group group=new Group();
Scene scene = new Scene(group);
mystage.setTitle("MyButton Graphics");
mystage.setWidth(350);
mystage.setHeight(300);

VBox vbox = new VBox();
vbox.setLayoutX(100);
vbox.setLayoutY(100);

Image myimg = new Image(getClass().getResourceAsStream("myimage.png"));
Button mybutton = new Button("My Button");
mybutton.setGraphic(new ImageView(myimg));
mybutton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("Done......");
}
});

vbox.getChildren().add(mybutton);
vbox.setSpacing(10);
((Group)scene.getRoot()).getChildren().add(vbox);

mystage.setScene(scene);
mystage.show();
}
}
 JavaFX Button Example Output :
JavaFX Button Graphics Example
 More JavaFX GUI Tutorials :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s