setting native desktop look and feel with java

This morning I woke up still thinking in Java and wondering why I have to live with the positively ancient and hideous Metal Look-and-Feel (LaF). Turns out I don’t have to. As you can see above, I’ve managed to turn on the Windows 8.1 LaF on my simple test framework, with just a few extra lines of code. And I’ve re-discovered it’s portable across all the environments I want to run this on (at least, the ones I care about), primarily Windows 7 and 8 and various Linux distributions and desktops. But first, the code. This is a modification to the source file TabbedTables.java, first seen in the prior post.

private static void createGUI() {try {// Set system Java L&F (Windows for Windows, Gnome for Linux...)//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch (UnsupportedLookAndFeelException |ClassNotFoundException |InstantiationException |IllegalAccessException exception) {// silently handle exception}JTabbedPane tabbedPane = new JTabbedPane();ImageIcon icon = createImageIcon("images/image.gif");tabbedPane.addTab("Tab 1", icon, makeTable(), "Sample 1");tabbedPane.setTabComponentAt(0, new CloseTabControl(tabbedPane));tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);tabbedPane.addTab("Tab 2", icon, makeTable(), "Sample 2");tabbedPane.setTabComponentAt(1, new CloseTabControl(tabbedPane));tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);JFrame frame = new JFrame("Test tabs and tables");frame.setContentPane(tabbedPane);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(800, 600);frame.setVisible(true);}

Lines 2 through 13 are the new lines added. If you’re lazy and don’t care about defensive programming practices then you can toss the try/catch block and just copy lines 5 and 6 and be done with it. But I prefer to write a little more robustly if possible, so that try/catch block is a very small price to pay. I believe in quality code, especially in the final shipping product. In the process of adding this code block, I also removed a line of code; JFrame.setDefaultLookAndFeelDecorated(false);  . This line is no longer needed.

This code block also illustrates an interesting feature of NetBeans 8. The catch block (lines 8 through 13) is a multiple exception catch block that was introduced in Java 7. In earlier versions of Java you needed to write separate catch blocks for every exception, an onerous task, even if you had an IDE that would automatically assist you in creating them all. Once written you had all those individual catch blocks to maintain into the future. It’s no wonder that a lot of Java programmers eschewed writing try/catch unless it was absolutely necessary (as in, javac refused to compile the code). While this doesn’t remove all the pain, multiple exception catch makes it a lot easier to write, and in particular, easier to read in the future.

How does NetBeans 8 fit into this? I was given a recommendation by the NetBeans 8 IDE, and when I selected that from its dialog, it automatically reformatted the code for me. This is one key reason why a good IDE like NetBeans or IntelliJ IDE are at times vital to fast and accurate Java programming. Those tools support you in writing up-to-date compliant code, and teach you a few things along the way.

Here’s a few screen captures of the exact same code running on Linux. Both were running in VMware. The top screen capture is CentOS 6.5 (equivalent to RHEL 6.5) running Gnome 2, and the bottom is Linux Mint 15 running its Cinnamon desktop. Of note are the tabs. They don’t look quite right, and that’s because of the “hand-drawn” close button icon. I need to dig a little deeper and learn how to use a given LaF icon, using the “hand drawn” version when I can’t find it. That will make the tabs look a lot better. But for the time being, this is Good Enough.

NOTE: All my Linux installations (hardware and virtual) are now running Java 8. Even my little Raspberry Pi.

i’ve resurrected my old sourceforge account

Back in 2000 I was writing a Java-based portable environment I called Portable Java Environment, or PJE. Catchy name, right? My goal back then was to use Java to replace the regular Unix desktop, using only the lowest level X libraries for displaying the graphics portion. When it wasn’t running on Unix (Solaris) or Linux, it would be running on Windows as a regular application, but with all the same tooling. Thus you could switch back and forth, be reasonably productive, and not worry about differences between desktop environments. Such were the dreams back then…

The main component of that environment was the desktop which I called J-Desktop. I started to share it online, and the name morphed into Jesktop (see here and here). I tried to keep it going, but the job pressures mounted and then 9/11 came along and I drifted away from the project.

Flash forward to 2014, and I’m in need of a place to put the Arch Linux ARM images I’ve been slowly piecing together for others to download. So today I create Project Rubus and uploaded both the base and base+twm 8GB images (compressed via zip) up to Sourceforge.

The circumstances between 2000 and now are very different. My girls are now in their mid-20s and out on their own, and my job responsibilities have changed drastically. Although, ironically, I’m back at the same facility I was working at in 2000, on the same program. But the circumstances are radically different, and the program has long since shipped, and is in use.

I’ve added my Sourceforge project link to the top menu. So if you don’t want to read my rants you can go straight there and see what I’ve added to the project. Note that I’ll always blog about it here, so it might be useful to read what I have to say about the code or tools before you grab you copy.

The project has a wiki, and I usually put small announcements there. I did this time for the Groundhog Day Image Release.

As for that PJE/J-Desktop tooling, I wonder now if it would run sufficiently well on the ARM-specific java that’s available from Oracle. It’s been fourteen years since I started that project, and Java has changed drastically, especially its performance (it’s increased drastically). I’m not too crazy about any of the desktop environments that are available to run on the Raspberry Pi. Sorta makes me wonder…