I'm a beginner of JavaFX. Is there anyway to change the color of title bar. I just want to make the application all black. I have searched the JavaFX API, but I can't find any method to make it.
-
See the following links which are for JFrame and Compose Multiplatform repectively: - https://github.com/kalibetre/CustomDecoratedJFrame - https://github.com/JetBrains/compose-multiplatform/issues/3388#issuecomment-1672732504 – Mahozad Aug 16 '23 at 15:34
2 Answers
This is pretty much an OS dependent thing. The operating system gets to decide how titlebars and borders are displayed by default. However, it definitely can be done, if you are willing to venture into making your own title bar.
To start out, you will need to modify the application window's stage to StageStyle.UNDECORATED
. Then you will need to set up your own borders and title bar (complete with things like the title, minimize button, close button, etc.). You can then add the border pane as a scene into your application's stage, and that should render your custom titlebar then without the default Windows styling.
You can find an example implementation of that here: https://stackoverflow.com/a/9864496/2694511
Do note that because you are implementing your own titlebar, you will also lose the OS's default window drag behavior, so you will need to implement your own window-drag/window-move code. This answer might be able to help you with that: https://stackoverflow.com/a/11781291/2694511
-
3setting stage to undecorated removes window 10 support for quick edge resizing – PathToLife Jan 20 '18 at 13:14
-
1is there a simple way to hack this in `JNA.platform.win32`? If all I want is [`SetSysColors`](https://learn.microsoft.com/en-ca/windows/win32/api/winuser/nf-winuser-setsyscolors?redirectedfrom=MSDN)? – Groostav Sep 16 '19 at 08:24
-
1I doubt that it's only an OS dependent thing. Otherwise the title bar would be dark in my case, since I've set my OS theme to dark and the title bars of all other apps look dark. – Stefan Endrullis Feb 08 '22 at 09:33
There is a Windows API allowing you to set the color of the window title bar: DwmSetWindowAttribute and how it can be used.
With JNA you can access this API like this:
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef;
import javafx.stage.Stage;
import javafx.stage.Window;
import lombok.val;
public class FXWinUtil {
public static WinDef.HWND getNativeHandleForStage(Stage stage) {
try {
val getPeer = Window.class.getDeclaredMethod("getPeer", null);
getPeer.setAccessible(true);
val tkStage = getPeer.invoke(stage);
val getRawHandle = tkStage.getClass().getMethod("getRawHandle");
getRawHandle.setAccessible(true);
val pointer = new Pointer((Long) getRawHandle.invoke(tkStage));
return new WinDef.HWND(pointer);
} catch (Exception ex) {
System.err.println("Unable to determine native handle for window");
return null;
}
}
public static void setDarkMode(Stage stage, boolean darkMode) {
val hwnd = FXWinUtil.getNativeHandleForStage(stage);
val dwmapi = Dwmapi.INSTANCE;
WinDef.BOOLByReference darkModeRef = new WinDef.BOOLByReference(new WinDef.BOOL(darkMode));
dwmapi.DwmSetWindowAttribute(hwnd, 20, darkModeRef, Native.getNativeSize(WinDef.BOOLByReference.class));
forceRedrawOfWindowTitleBar(stage);
}
private static void forceRedrawOfWindowTitleBar(Stage stage) {
val maximized = stage.isMaximized();
stage.setMaximized(!maximized);
stage.setMaximized(maximized);
}
}
The code allows you to activate the dark mode of the window title bar via:
FXWinUtil.setDarkMode(stage, true);

- 4,150
- 2
- 32
- 45