Skip to content

YetiHafen/javafx-customcaption

Repository files navigation

javafx-customcaption

javafx-customcaption is designed to allow customizing the native window caption on Microsoft Windows

Usage:

You can use the following code to remove the default caption while re-adding native looking window controls

public class HelloApplication extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        // initialize empty window
        Scene scene = new Scene(new Pane());
        stage.setScene(scene);
        stage.setTitle("customcaption-demo");
        stage.show();
        
        // this will remove the default caption and add 
        // native looking controls
        CustomCaption.useForStage(stage);
    }
}

When using CustomCaption.useForStage() you can pass an additional argument specifying additional information about the controls that will be drawn.

Example:

CustomCaption.useForStage(stage, new CaptionConfiguration(
        40, // caption height
        Color.BLUE, // control icons (foreground) color
        Color.AQUA // control buttons (background) color
));

// or alternatively

CustomCaption.useForStage(stage, new CaptionConfiguration()
        .setCaptionHeight(40)
        .setIconColor(Color.BLUE)
        .setControlBackgroundColor(Color.AQUA));

You can remove the caption entirely by setting useControls to false

Note that you should define your own drag region (as explained below) if you're using this, otherwise your window will not be draggable

Custom DragRegion

The class DragRegion allows you to describe the area where the window should be draggable while allowing the exclusion of specified areas to keep buttons etc. functional.

DragRegion region = new DragRegion(base);   // base is the main element that should be draggable
region.addExcludeBounds(node);              // node is an element e.g. button that is not
                                            // supposed to be draggable

The final DragRegion can be passed to the CaptionConfiguration

CustomCaption.useForStage(stage, new CaptionConfiguration()
        .useControls(false)
        .setCaptionDragRegion(dragRegion));

To simplify this process there are multiple options for CaptionConfiguration.setCaptionDragRegion() a complete list can be found here

Example:

The following example uses the Complex Application Template provided by Gluon Scene Builder

public class HelloApplication extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        
        // initialize FXMLLoader
        FXMLLoader loader = new FXMLLoader(getClass().getResource("complex-application.fxml"));
        // load the contents of the fxml file
        Parent root = loader.load();
        
        // create scene with loaded contents
        Scene scene = new Scene(root);
        
        // start stage with specified scene
        stage.setScene(scene);
        stage.setTitle("customcaption-demo");
        stage.show();

        
        // get MenuBar to supply as DragRegion
        MenuBar bar = (MenuBar) loader.getNamespace().get("menuBar");

        // apply customizations
        CustomCaption.useForStage(stage, new CaptionConfiguration()
                .setIconColor(Color.BLACK)  // set the icon/foreground color to black
                .setCaptionDragRegion(bar)  // set the MenuBar as DragRegion to exclude the
                                            // buttons automatically
        );
    }
}

Here is a small demo of the final program:

example

Get this library:

This library is available on maven central include it as a dependency for your project like this:

(replace "TAG" with the current version shown above or visit this page for more information)

maven:

<dependency>
    <groupId>net.yetihafen</groupId>
    <artifactId>javafx-customcaption</artifactId>
    <version>TAG</version>
</dependency>

gradle:

dependencies {
    implementation 'net.yetihafen:javafx-customcaption:TAG'
}

About

Custom captions (window decorations) on Windows for JavaFX

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published