0

Netbeans recently added the FlatLaf look and feel which I would love to add to our Netbeans Platform (RCP) application. I saw a lot of threads describing how to add a look and feel to a simple java application, but the few that were concerned with adding it to a netbeans rcp application did not really offer a solution or had only dead links.

If I activate the "Tools -> Options -> Appearance" menu-entries in our application I can already activate the FlatLaf manually but I obviously would like to automatically do that when the application starts.

Does anyone know how to do this?

(Project is running under JDK11 and Netbeans RCP dependencies with version RELEASE113 (11.3) )

Daki
  • 97
  • 9

2 Answers2

2

Add the following code in the validate() method of a ModuleInstall subclass, so that it's done very early during the startup process.

NbPreferences.root().node("laf").put("laf", FlatDarkLaf.class.getName());
UIManager.installLookAndFeel(new UIManager.LookAndFeelInfo("FlatLaf Dark", FlatDarkLaf.class.getName()));  

To switch back to the default theme:

NbPreferences.root().node("laf").remove("laf");

For more complete code have a look to my application JJazzLab-X on GitHub, in the UISettings Netbeans module.

Rangi Keen
  • 935
  • 9
  • 29
jjazzboss
  • 1,261
  • 8
  • 14
  • Thanks for the tip but sadly it didn't work for me. I put those two lines of code into the overridden validate() method of an already existing class (`public class Installer extends ModuleInstall`). After that didn't work I also tried: `NbPreferences.root().node("laf").put("laf", "com.formdev.flatlaf.FlatDarkLaf"); FlatDarkLaf.install(); UIManager.setLookAndFeel(new FlatDarkLaf());` (following getting started on the formdev website) but that didn't work either.. No errors or anything by the way just not showing the flatlaf when I start the application. – Daki Dec 10 '20 at 11:12
  • Strange. What version of Netbeans ? The 2 lines of code works for my RCP app (JDK 11 and Netbeans 12). Did you try to debug to make sure the code is called ? Conflict with other code you left to set up the L&F ? – jjazzboss Dec 10 '20 at 20:45
  • It really is. We're using Netbeans RELEASE113 as dependency version, Netbeans IDE 12.2 to run/debug it (if that matters). I also thought it might not be executed at all, but debugging showed that it actually did get executed. Other code with regards to L&F doesn't exist in the application (to my knowledge). There is another `Installer` class extending `ModuleInstall` in another nbm package though. But I tried adding this `validate()` method there as well and it didn't change the outcome either. – Daki Dec 11 '20 at 09:24
  • Well, it seems the only difference with my working app is the Netbeans platform module... Can you try to use the Netbeans 12 as dependency version instead of 11.3? – jjazzboss Dec 11 '20 at 19:53
  • Sadly enough I can't do that at the moment. I would have to rebuild dozens of modules because they all depend on netbeans api and in my experience upgrading the netbeans version (especially with a major version) will create troubles that need fixing. However I will keep your solution in mind and when we update to Netbeans 12 or up I will most certainly try it out. As far as I can recall, in the Netbeans IDE the option for the FlatLaf L&F was only introduced with version 12+ so I believe there might be a good chance that you're correct and this will work with Version 12+. – Daki Dec 14 '20 at 09:40
  • Thanks for your help so far though! If anyone else has a solution that might already work with Netbeans 11.3 I would still appreciate it very much. – Daki Dec 14 '20 at 09:41
  • OK. Can you mark my post as an answer ? Or at least mark it useful ? Thanks – jjazzboss Dec 14 '20 at 20:50
  • I already marked your answer as useful but sadly my votes aren't shown yet because I'm missing 2 Reputation for that ability ... sorry! I would mark it as an answer but I don't know yet if it works for sure. So I would prefer to wait until I can confirm it works here as well. As soon as I can do that I will mark it as an answer of course. – Daki Dec 14 '20 at 21:03
  • I was finally able to try out your solution with Netbeans RELEASE122 dependency. Sadly enough it still had no effect on the L&F of our application. But this time I get an error message : ```WARNING [org.netbeans.ProxyClassLoader]: Will not load class com.formdev.flatlaf.FlatDarkLaf arbitrarily from one of ModuleCL@65cbd5ba[org.netbeans.libs.flatlaf] and Module [...] starting from SystemClassLoader[93 modules];``` I'll try to look into this some more.. – Daki Feb 01 '21 at 13:33
  • FYI another user tried my answer, he managed to make it work, see https://stackoverflow.com/questions/65698853/netbeans-platform-how-to-style-the-netbeans-gui-components-programmatically/65707864#65707864 – jjazzboss Feb 01 '21 at 19:57
  • Thanks for the tip! Activating LAF during runtime might come in handy someday. I just figured out that I was missing the dependency to org-netbeans-libs-flatlaf. Having added that, your original solution works just fine with both RELEASE113 and RELEASE 122 versions of Netbeans dependencies. So thank you very much for your help! – Daki Feb 04 '21 at 10:07
  • Only Problem remaining is that when using FlatDarkLaf the branded application icons are not shown anymore. Instead the standard Netbeans icon is shown. This also applies to the tray-icon. This error doesn't occur however with the FlatLightLaf version... Not the biggest problem though because light version was my main interest. – Daki Feb 04 '21 at 10:10
  • 1
    For branding icons and splashcreen, when using Dark laf Netbeans search for different file names, ie frame_dark.gif instead of frame.gif, frame32_dark.gif, splash_dark.gif etc. So you need to manually create those files in the same branding directory. – jjazzboss Apr 10 '22 at 08:37
  • That makes sense, thanks for the tip! Next time I'm working on that module I'll add those icons and try it out. – Daki Apr 19 '22 at 08:34
0

To anyone who might want to try the solution of jjazzboss: It works like a charm so long as you add the following dependency (which I forgot):

<dependency>
    <groupId>org.netbeans.api</groupId>
    <artifactId>org-netbeans-libs-flatlaf</artifactId>
    <version>${netbeans.version}</version>
</dependency>
Daki
  • 97
  • 9