Rohit Potter
Rohit Potter

Reputation: 145

How to disable the maximizing option in JavaFX?

I was creating a JavaFX program. I have used .fxml codes to design the page but there is a problem, whenever I run the program it works fine with the default height and width but when I maximize that result, the buttons and other contents remain at their original position of default height and width. They didn't adjust with the height and width. Because it is an mp3 player and can perform some other minor functions, so I want that its size should not be expandable (it can not be maximized). This will solve my problem too.

.fxml code

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<GridPane alignment="top_left" hgap="10" prefHeight="475.0" prefWidth="800.0" style="-fx-background-color: white;" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
      <RowConstraints />
   </rowConstraints>
   <children>
      <Label prefHeight="49.0" prefWidth="800.0" text="Spotify Downloader Music Player">
         <font>
            <Font name="Comic Sans MS" size="24.0" />
         </font>
         <GridPane.margin>
            <Insets left="10.0" />
         </GridPane.margin></Label>
      <GridPane GridPane.rowIndex="1">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="392.0" minWidth="10.0" prefWidth="253.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="551.0" minWidth="10.0" prefWidth="391.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="551.0" minWidth="10.0" prefWidth="217.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Button mnemonicParsing="false" prefHeight="30.0" prefWidth="275.0" text="Home">
               <font>
                  <Font size="14.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" prefHeight="45.0" prefWidth="257.0" text="Playlist" GridPane.rowIndex="1">
               <font>
                  <Font size="14.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" prefHeight="47.0" prefWidth="257.0" text="Settings" GridPane.rowIndex="2">
               <font>
                  <Font size="14.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" prefHeight="51.0" prefWidth="257.0" text="About" GridPane.rowIndex="3">
               <font>
                  <Font size="14.0" />
               </font>
            </Button>
            <Label prefHeight="33.0" prefWidth="253.0" text="Music name" GridPane.columnIndex="1">
               <font>
                  <Font size="14.0" />
               </font>
               <GridPane.margin>
                  <Insets left="10.0" />
               </GridPane.margin>
            </Label>
            <TextField prefHeight="34.0" prefWidth="543.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
               <font>
                  <Font size="14.0" />
               </font>
               <GridPane.margin>
                  <Insets left="10.0" />
               </GridPane.margin>
            </TextField>
            <Button mnemonicParsing="false" text="Search" GridPane.columnIndex="2" GridPane.rowIndex="1">
               <GridPane.margin>
                  <Insets left="10.0" />
               </GridPane.margin>
            </Button>
         </children>
      </GridPane>
   </children>
</GridPane>

How to disable the maximizing option in JavaFX?

How to change the contents position with size expansion?

Upvotes: 0

Views: 2226

Answers (2)

Slaw
Slaw

Reputation: 45806

If you set the resizable property of Stage to false it disables the maximize button while leaving alone the minimize button.

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        // Create Scene and add it to primaryStage
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}

This only stops the user from resizing the Stage but doesn't stop you (the developer) from resizing the Stage programmatically.

Defines whether the Stage is resizable or not by the user. Programatically you may still change the size of the Stage. This is a hint which allows the implementation to optionally make the Stage resizable by the user.

The Javadoc indicates this is only a hint to the implementation. I could only test this on a Windows 10 machine, however, but you mention in the question comments it works on a Linux machine.

Upvotes: 2

purring pigeon
purring pigeon

Reputation: 4209

You can use a utility window which will remove the maximize and minimize options from the toolbar.

stage.initStyle(StageStyle.UTILITY);

Or you could set a max height and width on the stage. For example:

stage.setMaxHeight(400.0);
stage.setMaxWidth(600.0);

However users may be confused if they have the option to maximize, but the window only goes to a specific height.

Upvotes: 0

Related Questions