JavaFX Square Button

JavaFX Square Button

The square buttons can be created by extending the ToolBar class in Java.

This tutorial demonstrates how to create a square button in JavaFX.

Create Square Button in JavaFX

Buttons are created by instantiating the Button class in JavaFX. The buttons are used to execute some activities in JavaFX.

It is represented by JavaFX.scene.control.Button class. The button can have a text or icon on it.

We need to set the button’s size to create the square button. The following functions are used to manipulate the size of the button in JavaFX.

Functions:

Java
 javaCopybutton.setMinWidth() button.setMaxWidth() button
    .setPrefWidth()

        button.setMinHeight() button.setMaxHeight() button
    .setPrefHeight()

        button.setMinSize() button.setMaxSize() button.setPrefSize()

We can use the methods above to set the size of a button to make it square. Follow the steps below to create a square button in JavaFX:

Full Source Code:

Java
 javaCopypackage delftstack;

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class JavaFX_Square_Button extends Application {
  @Override
  public void start(Stage Demo_Stage) {
    Button Button1 = new Button("One");
    Button Button2 = new Button("Two Two");
    Button Button3 = new Button("Three Three Three");

    Square_Button_ToolBar Tool_Bar = new Square_Button_ToolBar();
    Tool_Bar.getItems().addAll(Button1, Button2, Button3);

    BorderPane Border_Pane = new BorderPane();
    Border_Pane.setTop(Tool_Bar);

    Scene Demo_Scene = new Scene(Border_Pane, 500, 500);
    Demo_Stage.setScene(Demo_Scene);
    Demo_Stage.show();

    Tool_Bar.requestLayout();
  }

  // A derivative of the ToolBar class to resize all buttons of the same size and square.
  class Square_Button_ToolBar extends ToolBar {
    @Override
    protected void layoutChildren() {
      double Min_Pref_Size = Calculate_Pref_Child_Size();
      for (Node x : getItems()) {
        if (x instanceof Button) {
          ((Button) x).setPrefWidth(Min_Pref_Size);
          ((Button) x).setPrefHeight(Min_Pref_Size);
        }
      }
      super.layoutChildren();
    }
    private double Calculate_Pref_Child_Size() {
      double Min_Pref_Size = 0.0d;
      for (Node x : getItems()) {
        if (x instanceof Button) {
          Min_Pref_Size = Math.max(Min_Pref_Size, x.prefWidth(-1));
        }
      }
      return Min_Pref_Size;
    }
  }
  public static void main(String[] args) {
    launch(args);
  }
}

The code above will create the square button of the size in which the text is fitted.

Output:

JavaFX Square Button

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java JavaFX