Simple swing (UI L and F) problem

J

James Kimble

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....
 
R

Rhino

James Kimble said:
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
 
J

James Kimble

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top