Why do Java's GtkLookAndFeel Popups have no border?
Asked Answered
L

3

6

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?

Lava answered 18/7, 2009 at 22:16 Comment(0)
D
2

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.

Dygall answered 19/7, 2009 at 3:28 Comment(1)
But if it's following a .gtkrc, the popups should have a border and shadow, since all of the other native apps do.Lava
U
2

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.

Understanding answered 29/8, 2011 at 18:9 Comment(0)
B
1

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.
Bwana answered 4/11, 2015 at 14:55 Comment(1)
If the bash-script URL does not work (GDPR issues) use my Github account at github.com/haniibrahim/swing-menu-corr.Bwana

© 2022 - 2024 — McMap. All rights reserved.