How to force / use GTKLookAndFeel in Java on KDE?

First of all, using gnome is not an option (but it is possible to install its libraries).

I need to know what is needed to display the Java Swing desktop using the current KDE installed KDE look. Ideally, the solution should allow me to apply an appearance that looks like a basic window system (e.g. Windows LNF for Windows, GTK LNF for Gnome (GTK), QT LNF for KDE (QT), by default for other platforms).

In KDE, you can configure it to use the current KDE theme for GTK applications. So, if the solution works with GTK, that's fine.

When I run the following code snippet under Gnome (Ubuntu 8.04), the Java application looks beautiful. It integrates very well with other applications:

try { // Set System L&F UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { //Handle it } 

However, if I run the same in Debian (Lenny) with KDE, calling UIManager.getSystemLookAndFeelClassName () returns the default Java value. If I continue and force him to use GTK LNF, the application does not work. Some fields are invisible, others become inappropriate, everything is unusable:

 try { //Force the GTK LNF on top of KDE, but **it doesn't work** UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); } catch (Exception e) { /*Handle it*/ } 

I also tried putting the following code. This allowed the user to select any of the available LNFs, and then try to install it. Metal and motif work great. GTK does not. The slider is really messed up. The list box looks ugly and disappears, but it seems to work. The buttons and menus look fine. The corresponding code is shown here:

 (...) /** Creates new form SwingFrame */ public SwingFrame() { initComponents(); //Save all available lafs in a combobox cbLafs.removeAllItems(); UIManager.LookAndFeelInfo[] lafs=UIManager.getInstalledLookAndFeels(); for (int i=0,t=lafs.length;i<t;i++) { cbLafs.addItem(lafs[i]); System.out.println(lafs[i].getName()); } } public void changeLookAndFeel(String laf) { //If not specified, get the default one if (laf==null) { laf=UIManager.getSystemLookAndFeelClassName(); } try { // Set System L&F UIManager.setLookAndFeel(laf); } catch (Exception e) { // handle exception e.printStackTrace(); } SwingUtilities.updateComponentTreeUI(this); } private void cbLafsActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: UIManager.LookAndFeelInfo laf=(UIManager.LookAndFeelInfo)cbLafs.getSelectedItem(); if (laf==null) changeLookAndFeel(null); else changeLookAndFeel(laf.getClassName()); } 

In the same system, all GTK applications work (for example, Firefox), as expected. So:

1) What is missing in the environment for the Java GTK LNF application to work in KDE?

2) What does the JVM check for returning GTK as the system default theme?

Thank you for helping Luis Fernando

PS-> I also tried other solutions like JGoodies, simple AWT and SWT. However, Swing with GTK LNF will be the best solution to avoid problems with native SWT libraries and additional JGoodies banks (also JGoodies LNF does not look as integrated as Swing GTK under Gnome). AWT looks disgusting (similar to a motive) and misses a lot of features.

+4
source share
4 answers

You can set the look from the command line:

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

In addition, SwingSet2.jnlp provides an example demonstration of all the various things that can be changed. History and other information can be found here: link text

+2
source

maybe this works:

 try { // sure look and feel UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); // not-so-sure look and feel System.setProperty("os.name", "Windows"); System.setProperty("os.version", "5.1"); UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception ex) { ex.printStackTrace(); } 
+1
source

Indication of documentation:

  • If the swing.defaultlaf system property swing.defaultlaf not null, use its value as the default appearance class and style name.

  • If the swing.properties property swing.properties exists and contains the swing.defaultlaf key, use its value as the default appearance class and style name. The place that is checked for swing.properties may vary depending on the implementation of the Java platform. In the Sun implementation, the location is ${java.home}/lib/swing.properties

Refer to the release notes, which are used for further details.

But I'm 99% sure your problem is this (quoting the docs again):

After changing the appearance, it is imperative to call updateUI for all JComponents . The SwingUtilities.updateComponentTreeUI(java.awt.Component) method simplifies applying updateUI to the containment hierarchy. Contact him for more information. The exact behavior of not calling updateUI after changing the appearance is not defined. It is very possible to get unexpected exceptions, problems with painting, or worse.

If you do not want to call updateUI on all JComponents , be sure to call UIManager.setLookAndFeel before every other swing code.

+1
source

GTK Laf is IMHO, a broken period. It does not perform some random settings. I believe that on most components you should not follow any setBackground (), setForeground () or setFont ().

If you use java> 1.4.2 I suggest using MetalLookAndFeel [should be UIManager.getCrossPlatformLookAndFeelClassName ()]. If you are using> 1.6.0_u10, you can try NautilusLookAndFeel. I personally think that metal looks better.

-1
source

All Articles