4

Since IntelliJ version 2019.2 Jetbrains removed the titlebar from the IDE and put the minimize, maximize and close button of the window into the menubar. So far, I have not found out how to do so using javafx. Is there a way to instanciate a "WindowControlButtons"-Class, so I can easily add them to the menubar or do I have to add a buttongroup and style the buttons for each platform myself?

Example how it shall look like on Windows:

example image

Slaw
  • 37,820
  • 8
  • 53
  • 80
Niklas
  • 105
  • 8
  • JavaFX doesn't have an API for this. Neither does Swing, as far as I know. It's possible IntelliJ is using https://learn.microsoft.com/en-us/windows/win32/dwm/customframe somehow. – Slaw Nov 26 '19 at 12:25
  • 1
    a possible option is to use a window without decoration. so the window layout is entirely in the hands of the programmer. – mr mcwolf Nov 26 '19 at 15:24
  • mcwolf's approach probably doesn't integrate well into the "standard window look" of the os, even it's just different versions of windows... – fabian Nov 26 '19 at 16:46
  • 1
    Quite right. The window decoration itself is made by the OS (window manager). Therefore, the custom solution will probably be different from the layout of the other windows, but it will be the same regardless of the platform. This may be an advantage or disadvantage, depending on the specific requirement. – mr mcwolf Nov 27 '19 at 07:58

1 Answers1

0

As suggested by @mr mcwolf You can try below solution.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class JavaFXApplication1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button closeButton = new Button();
        closeButton.setText("X");
        closeButton.setOnAction((ActionEvent event) -> {
            javafx.application.Platform.exit();
        });
        Button hideButton = new Button();
        hideButton.setText("-");
        hideButton.setOnAction((ActionEvent event) -> {
            primaryStage.setIconified(true);
        });
        Menu menu = new Menu("Menu");
        MenuItem menuItem1 = new MenuItem("item 1");
        MenuItem menuItem2 = new MenuItem("item 2");
        menu.getItems().add(menuItem1);
        menu.getItems().add(menuItem2);
        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().add(menu);
        HBox hBox = new HBox(menuBar, hideButton, closeButton);
        HBox.setHgrow(menuBar, Priority.ALWAYS);
        HBox.setHgrow(hideButton, Priority.NEVER);
        HBox.setHgrow(closeButton, Priority.NEVER);
        BorderPane root = new BorderPane();
        root.setTop(hBox);
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.show();
    }

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

}

Output on Windows looks like this.

enter image description here

Manthan Tilva
  • 3,135
  • 2
  • 17
  • 41