Simple swing (UI L and F) problem

Discussion in 'Java' started by James Kimble, Mar 23, 2005.

  1. James Kimble

    James Kimble Guest

    I've been given the task up updating the appearence (ie color
    when selected or not) of some JToggleButtons on a fairly large
    application and it seems like a really bad idea to do this via
    btn.setBackground or btn.setForeground etc... I know it should be done
    via a UIManager to uniformly set the look and feel of these buttons to
    something different than the default. The problem I
    have is that I have no idea how to do this and I haven't been
    able to find anything useful on the internet.

    The original author did some of this stuff but changing doesn't
    seem to change the appearance of the app and I have to wonder if
    it's doing anything at all.

    ---------------------------------------------------------------
    UIDefaults def = UIManager.getDefaults();

    Object buttonBorder =
    new UIDefaults.ProxyLazyValue(
    "javax.swing.plaf.basic.BasicBorders",
    "getButtonBorder");

    def.putDefaults(new Object[]
    {
    "Button.border", buttonBorder,
    "ProgressBar.selectionForeground", Color.black,
    "ProgressBar.selectionBackground", Color.black,
    "Panel.background", Color.gray,
    "Button.background", Color.gray,
    "Label.background", Color.gray,
    } );
    ------------------------------------------------------------

    Can someone give me a hint or point me to a useful resource
    for this type of thing. How hard can it possibly be?

    Any help much appreciated....
     
    James Kimble, Mar 23, 2005
    #1
    1. Advertising

  2. James Kimble

    Rhino Guest

    "James Kimble" <> wrote in message
    news:...
    >
    > I've been given the task up updating the appearence (ie color
    > when selected or not) of some JToggleButtons on a fairly large
    > application and it seems like a really bad idea to do this via
    > btn.setBackground or btn.setForeground etc... I know it should be done
    > via a UIManager to uniformly set the look and feel of these buttons to
    > something different than the default. The problem I
    > have is that I have no idea how to do this and I haven't been
    > able to find anything useful on the internet.
    >
    > The original author did some of this stuff but changing doesn't
    > seem to change the appearance of the app and I have to wonder if
    > it's doing anything at all.
    >
    > ---------------------------------------------------------------
    > UIDefaults def = UIManager.getDefaults();
    >
    > Object buttonBorder =
    > new UIDefaults.ProxyLazyValue(
    > "javax.swing.plaf.basic.BasicBorders",
    > "getButtonBorder");
    >
    > def.putDefaults(new Object[]
    > {
    > "Button.border", buttonBorder,
    > "ProgressBar.selectionForeground", Color.black,
    > "ProgressBar.selectionBackground", Color.black,
    > "Panel.background", Color.gray,
    > "Button.background", Color.gray,
    > "Label.background", Color.gray,
    > } );
    > ------------------------------------------------------------
    >
    > Can someone give me a hint or point me to a useful resource
    > for this type of thing. How hard can it possibly be?
    >
    > Any help much appreciated....
    >

    I chose to do a similar thing in one of my applets a while back. This is a
    fragment of my code which involves overriding the default attributes in the
    UIManager; this code works fine:

    /*
    * Set the colour of the selected tab in the tabbed pane. Set the
    colour
    * of scrollbars. See
    src/java/swing/plaf/basic/BasicLookAndFeel.java in
    * src.jar for definitions of many of the default colours used in
    Java.
    */
    UIManager.put("TabbedPane.selected", SELECTED_TAB_COLOUR);
    //ScrollBar.thumb -> knob
    UIManager.put("ScrollBar.thumb", SCROLLBAR_KNOB_COLOUR);
    //ScrollBar.background -> track
    UIManager.put("ScrollBar.background", SCROLLBAR_TRACK_COLOUR);
    //ScrollBar.thumbHighlight -> dimples on knob
    UIManager.put("ScrollBar.thumbHighlight",
    SCROLLBAR_KNOB_HIGHLIGHT_COLOUR);
    //control -> default colour for controls (buttons, sliders, etc.)
    UIManager.put("control", CONTROL_BACKGROUND_COLOUR);

    By the way, SELECTED_TAB_COLOUR and the other all-capitalized variable names
    all originate in an interface I built to store the colours which are
    supposed to be used in the GUI. You could imitate that technique or replace
    them with constants like Color.black, as in your example.

    Rhino
     
    Rhino, Mar 23, 2005
    #2
    1. Advertising

  3. James Kimble

    James Kimble Guest

    I ended up writing some code to print out the contents the UIDefaults
    table (it's a HashTable) and then just setting the relevant item
    to the value I wanted. Man, this should not have taken this long...

    Thanks for your suggestion.

    jk



    > I chose to do a similar thing in one of my applets a while back. This

    is a
    > fragment of my code which involves overriding the default attributes

    in the
    > UIManager; this code works fine:
    >
    > /*
    > * Set the colour of the selected tab in the tabbed pane. Set

    the
    > colour
    > * of scrollbars. See
    > src/java/swing/plaf/basic/BasicLookAndFeel.java in
    > * src.jar for definitions of many of the default colours

    used in
    > Java.
    > */
    > UIManager.put("TabbedPane.selected", SELECTED_TAB_COLOUR);
    > //ScrollBar.thumb -> knob
    > UIManager.put("ScrollBar.thumb", SCROLLBAR_KNOB_COLOUR);
    > //ScrollBar.background -> track
    > UIManager.put("ScrollBar.background",

    SCROLLBAR_TRACK_COLOUR);
    > //ScrollBar.thumbHighlight -> dimples on knob
    > UIManager.put("ScrollBar.thumbHighlight",
    > SCROLLBAR_KNOB_HIGHLIGHT_COLOUR);
    > //control -> default colour for controls (buttons, sliders,

    etc.)
    > UIManager.put("control", CONTROL_BACKGROUND_COLOUR);
    >
    > By the way, SELECTED_TAB_COLOUR and the other all-capitalized

    variable names
    > all originate in an interface I built to store the colours which are
    > supposed to be used in the GUI. You could imitate that technique or

    replace
    > them with constants like Color.black, as in your example.
    >
    > Rhino
     
    James Kimble, Mar 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Big Daddy

    Java 1.2 Swing vs. Java 1.5 Swing

    Big Daddy, Apr 15, 2005, in forum: Java
    Replies:
    2
    Views:
    509
    Pete Barrett
    Apr 16, 2005
  2. mkrause
    Replies:
    0
    Views:
    680
    mkrause
    May 6, 2005
  3. lizard
    Replies:
    0
    Views:
    1,784
    lizard
    Jan 30, 2006
  4. S.T
    Replies:
    2
    Views:
    592
  5. Knute Johnson

    Swing is dead! Long live Swing.

    Knute Johnson, Feb 16, 2012, in forum: Java
    Replies:
    32
    Views:
    3,819
    Daniel Pitts
    Feb 29, 2012
Loading...

Share This Page