Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Java's GtkLookAndFeel Popups have no border?

I have an already written Java Swing app (so no switching UI frameworks) that I would like to look at least decent using the GTKLookAndFeel. I've already accounted for things like font and component size differences across LookAndFeels, but one thing I can't figure out is why my popup menus have no borders at all. It appears they are using Synth borders which don't look GTK native. I would like to be able to support Metal, Windows, and GTK, but these popups look terrible. Is this a bug? What's a nice (non hacky) way to fix this without affecting the other LookAndFeels' borders which look fine?

like image 527
Matt H Avatar asked Jul 18 '09 22:07

Matt H


3 Answers

In my opinion GTK LaF is broken by design. It does not honor the programmers UI settings (font color borders background etc) for seemingly random JCOmponents. This is because it gets its settings from a .gtkrc file, not setXXX(...) calls. I'd stick with Metal for Linux and 'native' for windows.

Response:

The GTK+ look and feel can be customized by way of resource files. Swing's GTK+ look and feel uses the following algorithm to locate the resource files:

  1. If the system property swing.gtkthemefile exists, parse it and stop, for example: java -Dswing.gtkthemefile=/tmp/customTheme -jar SwingSet2.jar.
  2. If the file user.home/.gtkrc-2.0 exists, parse it and continue.
  3. Determine the user selected theme name (THEMENAME) by way of the desktop property gnome.net/ThemeName which is determined using XSETTINGS. If this property is null, use Default as THEMENAME. 1. If the file user.home/.themes/THEMENAME/gtk-2.0/gtkrc exists parse it and stop. 2. If the system property swing.gtkthemedir exists and the file swing.gtkthemedir/THEMENAME/gtk-2.0/gtkrc exists, parse it and stop. 3. If the system property swing.gtkthemedir doesn't exist and the file /usr/share/themes/THEMENAME/gtk-2.0/gtkrc exists, parse it and stop. 4. Lastly if swing.gtkthemedir is defined parse swing.gtkthemedir/THEMENAME/gtk/gtkrc, otherwise parse /usr/share/themes/THEMENAME/gtk/gtkrc.

One way GTK+ can be customized is by way of a theme engine. A handful of engines exist. In 1.4.2, Swing supports theme files for Default, pixmap, and bluecurve engines. We are investigating ways to open up the API to allow for the creation of additional GTK engines. Visit http://www.themes.org for examples.
- http://java.sun.com/j2se/1.4.2/docs/guide/swing/1.4/Post1.4.html

My guess is that java is taking its theme from another rc file OR the current border options aren't supported.

like image 76
KitsuneYMG Avatar answered Sep 18 '22 17:09

KitsuneYMG


I know the response is belated, but if anyone sees this question on google (like I did) then I find that this solution is better than nothing:

    if (UIManager.getLookAndFeel().getClass().getName().equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"))
        aPopupMenu.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray));

It doesn't seem to work for menus added to popup menus (even if you call the function on them), but it does show the border on the primary menu.

like image 20
Matt Eskridge Avatar answered Sep 19 '22 17:09

Matt Eskridge


There are incompatible rc-files.

Unfortunately the http://www.ailis.de/~k/ blog is closed.

Ubuntu/Gnome Shell: e.g. /usr/share/themes/Adwaita/gtk-2.0/gtkrc

style "menu" { 
xthickness = 1 
ythickness = 1 ... 
} 
... style "separator_menu_item" { 
xthickness = 1 
ythickness = 1 
... }

Update 11/30/2015: For Ubuntu/Unity refer http://blog.hani-ibrahim.de/en/ubuntus-swing-gtk-laf.html => Global menu, native fonts.

On Linux Mint there are the menus.rc files in the Mint-themes in /usr/share/themes/[mint-theme]/gtk-2.0/style

In section style “menu” change:

xthickness = 0
ythickness = 0
text[ACTIVE] = @base_color

to:

xthickness = 1
ythickness = 1
text[ACTIVE] = @fg_color

In section style “menu-item-separator” change the entry:

ythickness = 0

to:

ythickness = 1

I wrote a bash-script for Mint which does the job automatically. It fixes all 3 problems:

  • Missing menu border.
  • Wrong font color of activated checkbox and radiobutton menu items (the font has the same color as the background and appears invisible).
  • No menu separators.
like image 36
Hani Avatar answered Sep 21 '22 17:09

Hani