2

I'm new to programming, but I'm preparing to write a Java program. As I'm planning it, I'm trying to find the right GUI for it. I found this page with GUI options. I have two questions:

  1. Will these just plug into the Java GUI builder?
  2. How easy (or hard) is it to change the GUI look and feel after the program is built?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ChrisC
  • 1,329
  • 5
  • 18
  • 36

4 Answers4

9

Changing the look and feel of a program is as simple as:

UIManager.setLookAndFeel("fully qualified name of look and feel");

before any GUI-creating code has been created. So you can easily create all your GUI with your GUI builder and then simply call this at the start of your program.

(Note that some look and feels, like Substance, are very strict when it comes to threading issues, so you better make sure that all your GUI code is run from the event dispatching thread.)

As a suggestion, two very good looking (and professional looking enough) look and feels in my opinion are Substance and also Nimbus which will ship with later releases of the JRE as the default look-N-feel (at least that's the plan) and which can be downloaded separately from java.net.

So if you opt for Nimbus the code would be:

UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

And one example of Substance (Substance has many skins and themes, if you want to know more read their documentation in their site) would be:

UIManager.setLookAndFeel(new org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel());

Also note that you can use the cross platform skin (Metal) like this:

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

And finally if you are on windows you can use the system look and feel like this:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Savvas Dalkitsis
  • 11,476
  • 16
  • 65
  • 104
  • 3
    Note that UIManager.setLookAndFeel will throw a UnsupportedLookAndFeelException if it can't find said look and feel. – Powerlord Jul 22 '09 at 14:52
  • I needed to use UIManager .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel") to get Nimbus to work. Note the com.sun.swing package, not javax.swing. – AngerClown Jul 22 '09 at 16:07
  • this is why you have a new jre version which includes the Nimbus look and feel in the "non official" com.sun packages. You cannot rely on this package being available in the future. The path i gave you "javax.swing" is correct if you download Nimbus as an external library. And also this will be the official name once Nimbus is included in the jre as the official package. So if i were you i'd download the library and use that until Nimbus ships as the default lnf. – Savvas Dalkitsis Jul 22 '09 at 20:27
  • Savvas, you said, "Also note that you can use the cross platform skin (Metal)...". Are all Substance skins not cross-platform? How can I tell which aren't? – ChrisC Jul 23 '09 at 04:18
  • no i meant the default cross platform look and feel that ships with Java. If you have code that allows the user to change the look and feel at runtime (via a menu for instance) then you will want to include the default look and feel in his options.To learn how to change the look and feel at runtime check out the answer R.Bemrose posted... – Savvas Dalkitsis Jul 23 '09 at 13:12
3

Building on what Uri said, you may want to stick with one of the more well-known look and feels.

If you use Swing, you may want to look into using the Nimbus look and feel... it's included with Java 6u10 and newer. Nimbus is built on the Synth framework included with Java 5 and newer.

Of course, if the end user is using a lower version, it will throw an UnsupportedLookAndFeelException and default to whatever the JVM default is (usually the Metal/Ocean (cross-platform) theme).

As a side note, if you use Swing, you can switch which look and feel is being used on the fly. You just have to call

// "lnfName" is the look and feel name.
// "frame" is the window's JFrame.
// A try/catch block is omitted here.
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • Could I not somehow include the necessary libraries (or whatever) with the installer so the user sees the look and feel I intended when I developed it? – ChrisC Jul 22 '09 at 18:05
  • you can include the look and feel .jar file in the distribution and also make it part of the class path. Then the users will be able to use it. – Savvas Dalkitsis Jul 22 '09 at 20:28
  • Is including the .jar files not common practice in the case of a non-standard look and feel design? Else why would R. Bemrose (or anyone else) be concerned about UnsupportedLookAndFeelException(s) and it defaulting to the default look and feel? Or do I not understand? – ChrisC Jul 22 '09 at 23:31
  • When you include the jar file in your distro you include it in a folder where you store your libraries (usually it is not mandatory). You program then invokes the library (in this case the look and feel). But what happens if the library is missing for some reason? You will get an apropriate exception in this case. There are some look and feels which are supported only on some platforms. For instance you cannot use some lnfs on windows and others on MacOS and others on Linux. This is when you get an UnsupportedLookAndFeelException. – Savvas Dalkitsis Jul 23 '09 at 13:15
1

Ignoring the pro/con of choosing a look and feel (LAF), I'll add some of my experience which using them...

If you're using a third-party/cots componentslibrary (JIDE, SwingLabs/SwingX, etc.) you will have problems as UI classes won't always exist or be up to date with all of the LAFs. JIDE supports a few external LAFs (Alloy and Synthetica). Some LAFs also offer support for widgets; Substance supports some of JIDE and SwingX. The end result is an application that is partially skinned. Not pretty. Make sure your LAF supports all of you components. If you're pure Swing/AWT, no problems.

In light of this, we use Alloy for JIDE support, considering moving to Synthetica. We would love to use Substance, but it doesn't have any JIDE support.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
basszero
  • 29,624
  • 9
  • 57
  • 79
0

Generally speaking, third-party GUI libraries with "look and feel" are not very popular, which is possibly an indication of their quality.

Standard GUI libraries are inherently very complex, and while they support some look and feel tweaking, the tweaks are often minor or constrained.

I would recommend sticking to Swing or SWT, and seeing what you can do with their look and feel mechanism, but don't expect miracles.

Uri
  • 88,451
  • 51
  • 221
  • 321
  • Why are they not very popular? Did you see the ones I linked to? They look pretty nice. What quality problems would deter people from using them? – ChrisC Jul 22 '09 at 18:03