Search code examples
javaswingjbuttonlook-and-feelgtk2

Can we ape the GTK+ 2.0 button style in Java Swing?


The problem with Java Swing is the look and feels (Metal, Nimbus, GTK...), the worst out there compared to SWT, Windows, Mac and Gnome Tool Kit, and getting good looking widgets is utopist, yet I am asking the question though.

I need to know if there is a possibility to tweak a JButton to look like a default GTK button. Otherwise, can we give the JButton text a style (a shadow for example)?

public class myTweakedButton extends JButton {

// Override style attributes here.

}

Here is the style applied by GTK to the button widget:

style "button" {
    xthickness = 3
    ythickness = 3

    bg[NORMAL] = shade (1.07, "#cdcdcd")
    bg[PRELIGHT] = shade (1.09, "#cdcdcd")
    bg[ACTIVE] = shade (1.0, "#cdcdcd")
    bg[INSENSITIVE] = mix (0.25, @bg_color, "#e2e1e1")
    fg[INSENSITIVE] = "#9c9c9c"

    engine "murrine" {
        #contrast = 1.0
        border_shades = {1.04, 0.82}
        reliefstyle = 5
        shadow_shades = {1.02, 1.1}
        textstyle = 1
        glowstyle = 5
        glow_shade = 1.1
        #text_shade = 1.04
    }

This is how a Button looks like when using the default GTK Look and Feel provided by Java engine:

Java GTK LAF button

And here how a button looks like under Linux (GTK 2.0).

GTK Button Linux

The difference is actually a bad text hinting in Java GTK LAF, while GTK button text is styled with a discrete shadow.


Solution

  • there are two ways

    • write own JButton

    • but correct way (don't reinvent the wheel) use proper Look and Feel,

    • possible scenario

      1. go to Insubstantial

      2. download code source,

      3. import all classes to the IDE (2-15 min depends of PC HardWare)

      4. search for folder test, there is Check.java,

      5. run that and to try everything in JMenu Look and Feel, required download jar files for every good Custom Java Swing Look and Feels are accesible from JMenu

      6. nothing better around, all standard and good custom Swing's Look and Feel in one JFrame

      7. maybe better would be to use Substance together with SwingX, very good Custom implementation for Custom Swing, again nothing better around

      8. NOTICE: SwingX required more than basic Swing knowledge, Substance (my view) required update and customize some methods in original code too