Current operating systems (e.g. Windows, macOS) let the user decide if they want dark content on light blackground (classic) or rather light content on dark blackground (dark mode).
Is there something available in Java, so that my (client side) Java programs can automatically adjust their contrast polarity according to that OS user setting? I am using Swing, if that matters.
I found the system property apple.awt.application.appearance, but nothing for Windows and nothing operating system independent.
And also, is there a listener? Because that setting might change on runtime.
Motivation: inverse contrast polarity seems to inhibit myopia (Aleman et al. 2018-07-18 Science)
There seems to be a way.
First understand that the setting of the desktop mode (light vs dark) can be read from the Windows Registry: How to detect Windows 10 light/dark mode in Win32 application?
Then it is just a question how to obtain access to this registry setting from your java application, which is described here: Read/write to Windows registry using Java
There is a feature request (JDK-8235460) submitted in 2019.
In the meantime you might want to try jSystemThemeDetector (Apache-2 license). This library works on Windows, macOS and some Linux distributions.
OsThemeDetector.getDetector().isDark()
You can also add a listener to detect changes.
OsThemeDetector.getDetector().registerListener(isDark -> {
SwingUtilities.invokeLater(() -> {
if (isDark) {
// The OS switched to a dark theme
} else {
// The OS switched to a light theme
}
});
});
Note: For JavaFX you use Platform.runLater instead of SwingUtilities.invokeLater.
Related
I have an idea for a class project which involves changing the desktop background image at different times. I saw these questions:
Can I change my Windows desktop wallpaper programmatically in Java/Groovy?
Programmatically change the desktop wallpaper periodically
Change desktop background of MAC sytem using Java native access
So I know it can be done on a specific operating system. Is it possible to set it for different operating systems without writing separate programs?
You can just use:
String os = System.getProperty("os.name");
to determine what OS the app is running on, and decide what to do from there. Like so:
if (os.startsWith("Windows")) {
// includes all Windows versions
} else if (os.startsWith("Mac")) {
// includes all Mac OS versions
} else {
// all others
}
I suggest looking up all of the different values os.name can have to be able to handle as many as possible. You might want to use enums for these values instead of checking startsWith like I did. Here is a list of values you might want to consider (although not very up to date).
I have a Java app whose windows and internal components have animations that could slow down a less powerful computer. I know that all OSs have some form of animation preferences (In Windows, there are check boxes for "Animate controls and elements inside windows", in Linux, there are selections for Full, Basic, or No animations, and in OSX you can do things like enter "defaults write com.apple.dock workspaces-edge-delay -float 0.0; killall Dock" or "defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool NO" into the terminal). Is there any way to find out whether the user has animations enabled or disabled, so that I can conform to er preferences?
I would think, that since Java is Operating System agnostic, that there is no specific API, native or third-party, to do this (although there could be). You could use a combination of JNI and/or executing external commands and then interpret their outputs to determine if animations are enabled. To do this you would have to query the os.name system property and run the specific commands for that OS. I think this would be a fair bit of pain and you may just want to give the users an option to disable the animations.
Personally, I would prefer the option because I might turn of the OS animations because they are annoying but still may enjoy the animations in your application.
I have to read the high contrast mode of the native os and have to apply the settings on my product. Now for windows I have done some thing like this:
try {
Toolkit toolkit = Toolkit.getDefaultToolkit();
boolean highContrast = (Boolean)toolkit.getDesktopProperty("win.highContrast.on" );
}catch(Exception e) {
}
This works fine for Windows but I need the desktop property to read linux high contrast settings. Can anyone please tell me what would be the desktop property for linux?
I think there is no good method to check high contrast mode in Linux whenever you using Java or not (X11 itself have no high-contrast feature and Linux have a vast variety of UI frameworks and each of them may implement high contrast in it's own way).
Strictly speaking you'll have a bunch of problems with this in Windows too (see here, or here).
There are two possible options to solve the case: just use system colors in your UI using SystemColor class (if that's just what you need) or use the same class for analyzing a contrast between current foreground and background colors (if you need to know if system colors are high contrast). You can also check the system theme name, but it's quite unreliable method.
I remember having once seen a list of properties that could be set on Swing components to make them look more native on Mac OS X.
This included ways to mark "dirty" documents with the "dot" in the window close button, open dialogs as sheets (not sure about that, but sure would be nice) etc.
I know Apple has let Java down as a "primary" programming language, but as they recently updated Java and even offer Java 6, I wonder if there is a comprehensive and current list - ideally with examples - on what you can do to make Swing apps look better without much effort on the Mac.
After receiving some answers, I put this into community wiki mode and started the following list to be expanded if need be:
Technical Notes: Java - User Experience: Overview page on Apple's developer connection reference library (index page).
New Control Styles available within J2SE 5.0 on Mac OS X 10.5: Examples for Button styles etc. specific to Mac OS X 10.5 Leopard.
Java Runtime System Properties: Information on System properties that help you enable the Apple-style menubar at the top of the screen, give rendering hints for text anti-aliasing etc.
Mac OS X Integration for Java: Information on Menubar and Application menu, context menus, keyboard shortcuts and AppleScript
PDF "Java 1.3.1 Development for Mac OS X (Legacy)": 80 pages of information on various topics such as packaging applications. This is somewhat outdated.
The Quaqua site may be interesting as well. From the site:
"The Quaqua Look and Feel (Quaqua) is a user interface library for Java applications which wish to closely adhere to the Apple Human Interface Guidelines for Mac OS X. ... It runs on top of Apple's Aqua Look and Feel, and provides fixes and enhancements for it."
It has a fairly good user guide with examples as well.
The Java Development Guide for Mac OS X has a Mac OS X Integration for Java section that is probably what you're looking for.
You can check this PDF at Apple, but it's marked as legacy.
On some applications, I'm still using com.apple.macos.useScreenMenuBar to have the Java application use the Mac OS X menu bar, and it still works in Leopard.
My personal link collection:
From developer.apple.com:
Java Runtime Properties for Mac OS X (don't get scared by the "not recommended" popup, some of the information is still useful)
Apple Java Extensions
OS X Runtime Configuration Guidelines (not directly related to Java, but an interesting read anyway)
From Sun:
Bringing your Java Application to Mac OS X Part Three
same page at another location with working images: http://192.9.162.55/developer/technicalArticles/JavaLP/JavaToMac/
When clicking on some old links I'd been redirected to Java Design Guidelines and Java Guides, seems as though I have to update my links...
We have a multi-platform Java-based system that spawns Robohelp for its online user guide information. It runs on a variety of Windows and Linux flavors. The default Robohelp code that is relevant to our Linux-based systems has a hard-coded link to "netscape" in the command line that it builds to spawn a web browser to view the help files. This is, obviously, less than helpful on a system that does not have netscape installed.
What I would really prefer to do is to detect the Preferred Applications preferences from Java when running on Linux. On the Fedora 9 machine where I'm currently sitting, this is found under System -> Preferences -> Personal -> Preferred Applications. The very first setting is "Web Browser."
How would I detect that setting from Java code?
Have a look at java.awt.Desktop. That supports opening/editing/printing a file in the user's preferred program, opening a URL in the user's preferred browser, sending a eMail, ...