views:

1538

answers:

5

Is it possible to change java lookandfeel without modifying the program in a real-world environment?

I know that theoretically the answer is yes. I also created demo applications and changed the lookandfeel even at runtime and it worked. But when I created a real application with lots of controls lookandfeel change was not really working:

  • Different lookandfeels have different bugs (or strange features). For instance some of them creates the same style for JTextField and JFormattedTextField, some of them uses different styles. This change can be quite annoying for users.
  • Control sizes are not the same (using different fonts, different borders), so sometimes the window looks ugly after change.
  • Keyboard shortcuts can also be different.
  • ...

In my applications after lookandfeel change (actually instead of changing the lookandfeel I tried using the same lookandfeel with different skins) I always have to modify the application. Not a very big modification, but lots of small modifications which makes lookandfeel change difficult.

Am I just very unlucky with the lookandfeels I tried (currently using skinlf), or lookandfeel change is not that easy as in small demo applications?

UPDATE: I'm using Swing and changing to other GUI library is not possible for me right now.

UPDATE2: It seems to me the question was not clear. I do know how to change the lookandfeel. My questions is: Is it possible to create a real application where changing lookandfeel (or the skin of the lookandfeel) is an easy task? Currently if I change the lookandfeel it is not enough to change 2 or 3 lines, I have to review the whole application and fix quite a few problems caused by the lookandfeel change. dhiller suggested replacing skinlf with a better lookandfeel. Can you show me a skinable lookandfeel where skin replacement works better?

A: 

The look and feel should change fonts and behaveour, that's the purouse of the look and feel system: it should behave as the native OS, and different OS have different ways of displaying things. I must confess that the "Java" look and feel (called metallic or something) is generally more pretty than many standard OS versions (This may have changed its a few years since I last used Java). I reccomend you by default use the OS look and feel, because it's confusing if the, for instance, Open file menu does not behave as it does in every other program (windows users is unfortunately used to inconsistent gui, but Mac users expect all program to behave properly).

Stein G. Strindhaug
Of course I'd like to use a lookandfeel which behaves like the native OS (windows, in my case). But there are plenty lookandfeels for the same OS. Assume that my users get bored the current blueish design and prefer a green one with different icons.
asalamon74
Then you could add an option to change the default. Is the windows LAF blueish? Last time I checked it was ugly gray win 2000 style, if it now actually support the (eh. 2) themes of windows the user could just change his windows theme. Why should each program have its own theme anyway?
Stein G. Strindhaug
+1  A: 

Yes, there are different ways to start your application with a certain look and fell without modifying the code of your application. An easy way is to set a system property at startup of your application. E.g. if you have a shell script or batch file starting your application you just change the command from

java -jar yourapp.jar

to

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

-Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel will set the default look and feel to use to the GTKLookAndFeel.

There is also a way using a swing.properties file of your Java installation. For more details have a look at this.

reallyinsane
This does not answer my question. Could you please read my whole question.
asalamon74
+1  A: 

In general it is a good idea IMO to use the system look and feel to avoid confusing the user by introducing a new look when he's already accustomed to the platform look and feel. The platform look and feel can be achieved by the following code.

  UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

Anyway, see Changing the Look and Feel After Startup (java tutorial), that might help you.

BTW: Maybe you are using the wrong layout managers?

dhiller
A: 

If your final goal is native look and feel for each target OS, you can look at SWT. This will provide OS native widgets which will change along with the OS look and feel.

That way, the user does not need to change the options for your application, all they need to do is modify their OS theme and the changes will propogate automatically to your app.

James Van Huis
Ooops I forgot the mention that I'm using Swing, and changing to SWT (although it sounds interesting) is not an option for me now. I'll edit my question to show this information.
asalamon74
+1  A: 

Have you tried setting the look and feel on the UIManager? Just kidding. Seriously, though, if you change look and feel and the app does not look right, it's because you're not using the LayoutManagers in the correct way. The easiest-to -grok example of this is using a null layout manager. This approach will not be cross-platform (extreme example). As a development technique, you should be doing all your Swing concept tests using various lookAndFeels. Otherwise you'll be tempted to "make things work" in your app, which is not the point. Really understanding the layouts can be hard work.

That said, some things like keyboard shortcuts need special handling always. Menus on Mac need special handling. Etc... and then there's app packaging.

So yeah, Java is great in small desktop demos, and requires work to get a real app perfect.

Yar