4

I have been using Dr Java on Ubuntu 14.04 and the GUI components all look very dated compared to the ones on Mac or Windows. Is there some way of updating this, or are all Java components in Linux just a bit old?

I'm using the JDK 8.0 Compiler from Oracle if that helps.

Here are some JButtons in Ubuntu

Mike
  • 63
  • 1
  • 3

2 Answers2

4

See: https://stackoverflow.com/questions/209731/how-do-i-force-get-to-use-gtklookandfeel-in-java-on-kde

You can hint to the JVM to use a different "Look and Feel" (LAF). There are a number of shipped LAFs available to the VM, and a few that are specific to some operating systems (i.e. The Windows LAF is only available on Windows.)

I suspect you are seeing the "Metal" LAF, which is pretty ugly (and happens to be the default if the developer does not specify a different LAF for a GUI app). You can try to force the GTK (or Nimbus, if you have the right version of the VM) LAF as suggested in this link. It will be something like this:

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel YourAppName

You will have to substitute the application name for YourAppName. Or you might have to edit a file if your app is being started up via a Java launcher of some kind. The idea is that you want to add this Java define ("-Dswing.defaultlaf=...") to the VM arguments somehow.

Since you are using the Oracle Java 8 VM, you might even want to try the nice new javax.swing.plaf.nimbus.NimbusLookAndFeel.

Refs: How to Set the Look and Feel

  • I followed the instructions in that second link and used a try...catch to set the look and feel to Gtk. Looks a lot nicer now thanks.

    I am unsure as to how the 4 potential exceptions could be handled though. Setting the look and feel to metal if Gtk cannot be found seemed logical, but it cannot be done without using another try ... catch (inside of what is already a try...catch).
    – Mike Oct 23 '15 at 03:59
  • Just catch all Exceptions (this is one of the few times you really want to catch them all [or list and catch all the possible exceptions the static call can raise, if you want]) and default to Metal, which is guaranteed to always be present. –  Oct 23 '15 at 04:01
  • That is, the nested `try ... catch` block is just noise, but you can always just add a comment or log a "can't happen" error. –  Oct 23 '15 at 04:09
  • Cool, thanks. I changed the block to this so it will just catch all of them, seems to work and is a lot nicer to look at: `try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); } catch (Exception e) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel"); } catch (Exception er) { //Will never happen. } }` – Mike Oct 23 '15 at 04:46
  • I suppose if you want to be more elegant, you can iterate through `UIManager.getInstalledLookAndFeels()`. Or use the static helpers instead of the actual classnames: `UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName())` –  Oct 23 '15 at 04:52
1

I hope you need Java to use GTK look and feel. As jdv answered you can set it at the runtime or you can set it for all the Java swing applications using the following technique:

Step 1: In Java 8 this file is not available by default. So the given command will create a new one. According to your Java version, change the path.

gksu gedit /usr/lib/jvm/jdk1.8.0_60/jre/lib/swing.properties

Step 2: Add the following line in the file and save the file

swing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel

Now run the application, you will get a native allok and feel for all the swing applications.

Eventhough it is not required for your Calculator application, enabling Global Menu will improve the look and feel of Java applications. To enable Global menu:

Ste 1: Install Jayatana using the following set of commands:

From Ubuntu 15.04 it is installed by default.

sudo add-apt-repository ppa:danjaredg/jayatana
sudo apt-get update
sudo apt-get install jayatana

Step 2: Disable gloabl Jayatana configuration

sudo rm /usr/share/upstart/sessions/jayatana.conf

Step 3: When you run a Java application, use the following command

java -jar -javaagent:/usr/share/java/jayatanaag.jar <file-name>

Credits to:

How can I get a java apps to use the GTK+ theme?

Global Menu Support for Java Applications in Ubuntu

Gobinath
  • 3,212
  • 4
  • 23
  • 33