asalamon74
asalamon74

Reputation: 6170

Changing java lookandfeel without modifying the application

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:

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?

Upvotes: 4

Views: 5876

Answers (5)

Dan Rosenstark
Dan Rosenstark

Reputation: 69787

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.

Upvotes: 1

James Van Huis
James Van Huis

Reputation: 5571

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.

Upvotes: 0

Daniel Hiller
Daniel Hiller

Reputation: 3505

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?

Upvotes: 1

reallyinsane
reallyinsane

Reputation: 1232

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.

Upvotes: 2

Stein G. Strindhaug
Stein G. Strindhaug

Reputation: 5119

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).

Upvotes: 0

Related Questions