Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » change button's background color
change button's background color [message #466740] Tue, 17 January 2006 02:19 Go to next message
Eclipse UserFriend
Originally posted by: popeye.netcabo.pt

i want to change the background color of a button in my rcp app.

when an event occurs, i have that code to change the color:
...
filterButton.setBackground(Display.getDefault().getSystemCol or(SWT.COLOR_YELLOW));
...

then, i click in the button, the event is raised but nothing happens. what i shall do? or what i'm doing wrong?

thank you.
Marco
Re: change button's background color [message #466806 is a reply to message #466740] Tue, 17 January 2006 19:41 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
You can't set the background color of a button on Windows. The operating
system won't let you change it.

"Marco Ferreira" <popeye@netcabo.pt> wrote in message
news:3048169.1137464374441.JavaMail.root@cp1.javalobby.org...
> i want to change the background color of a button in my rcp app.
>
> when an event occurs, i have that code to change the color:
> ..
>
filterButton.setBackground(Display.getDefault().getSystemCol or(SWT.COLOR_YEL
LOW));
> ..
>
> then, i click in the button, the event is raised but nothing happens. what
i shall do? or what i'm doing wrong?
>
> thank you.
> Marco
Re: change button's background color [message #466809 is a reply to message #466806] Tue, 17 January 2006 20:01 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Although dirty the hack which could be done is to register a
paintlistener on the button and draw a rect with a Alpha-Color (e.g.
0.5) above the button.

Very dirty but it may work?

Tom

Steve Northover wrote:
> You can't set the background color of a button on Windows. The operating
> system won't let you change it.
>
> "Marco Ferreira" <popeye@netcabo.pt> wrote in message
> news:3048169.1137464374441.JavaMail.root@cp1.javalobby.org...
>
>>i want to change the background color of a button in my rcp app.
>>
>>when an event occurs, i have that code to change the color:
>>..
>>
>
> filterButton.setBackground(Display.getDefault().getSystemCol or(SWT.COLOR_YEL
> LOW));
>
>>..
>>
>>then, i click in the button, the event is raised but nothing happens. what
>
> i shall do? or what i'm doing wrong?
>
>>thank you.
>>Marco
>
>
>
Re: change button's background color [message #466901 is a reply to message #466806] Thu, 19 January 2006 02:35 Go to previous messageGo to next message
Edward Kuns is currently offline Edward KunsFriend
Messages: 14
Registered: July 2009
Junior Member
"Steve Northover" <steve_northover@ca.ibm.com> wrote in message
news:dqjha0$aq4$1@utils.eclipse.org...
> You can't set the background color of a button on Windows. The operating
> system won't let you change it.

I see this question and answer very frequently here. Why isn't this in the
JavaDoc? I'm willing to submit a patch to put it in the JavaDoc, if
necessary, but I have assumed that there is some reason that this kind of
information isn't already there.
Re: change button's background color [message #467376 is a reply to message #466740] Mon, 30 January 2006 18:19 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: kwpeck.mersoft.com

If you enable Windows XP Themes via a manifest file and you use a theme
with a rounded button look (or run your stuff on a Mac) you will see
that setting the background color of the button sets the color "around"
the button so the missing corners of a rounded button will take the
color you set. If you place a button on a none standard background you
have to do this to make it look right on a Mac or themed Windows box.

I realize this does not answer your question but the color you are
setting will affect some users.

Since Windows / Mac can be themed they decided to not allow you to
override the colors as what would that color be? What if the button uses
a gradient fill? Your button would look pretty crappy. Also you need to
set both FG and BG colors as you have no idea what color the theme is
using for the other and your yellow background could make the text
unreadable.

You could place your button in a Composite with a bit of a margin (say 2
pixels) and color that Composite to show the button as highlighted. Be
sure to set the background color the Composite and of the button so it
looks proper on all OS versions.

Not sure what you are doing with the button. Maybe if we know that we
can find another solution. If you don't care that the button looks
exactly like the OS button you can paint your own Canvas with colors,
images and text along with mouse tracking code.

Marco Ferreira wrote:
> i want to change the background color of a button in my rcp app.
>
> when an event occurs, i have that code to change the color:
> ..
> filterButton.setBackground(Display.getDefault().getSystemCol or(SWT.COLOR_YELLOW));
> ..
>
> then, i click in the button, the event is raised but nothing happens. what i shall do? or what i'm doing wrong?
>
> thank you.
> Marco
Re: change button's background color [message #467386 is a reply to message #466901] Mon, 30 January 2006 20:35 Go to previous messageGo to next message
Daniel Spiewak is currently offline Daniel SpiewakFriend
Messages: 263
Registered: July 2009
Senior Member
SWT javadoc is kept extremely simplistic by design. With the exception of very few cases, most of the javadoc is simply templated out for each widget. This is both a vice and a virtue. It's a problem because more people have to revert to forums (like this one), FAQs, and tutorials about escoteric stuff to figure out how to use a Button. It's a huge advantage because experienced developers can quickly get exactly the info they want about the widget (supported styles and events) without having to muck around in wordy tutorials embedded in the code.
Re: change button's background color [message #467414 is a reply to message #467386] Tue, 31 January 2006 05:06 Go to previous messageGo to next message
Edward Kuns is currently offline Edward KunsFriend
Messages: 14
Registered: July 2009
Junior Member
Daniel Spiewak wrote:
> SWT javadoc is kept extremely simplistic by design. With the exception of very few cases, most of the javadoc is simply templated out for each widget. This is both a vice and a virtue. It's a problem because more people have to revert to forums (like this one), FAQs, and tutorials about escoteric stuff to figure out how to use a Button. It's a huge advantage because experienced developers can quickly get exactly the info they want about the widget (supported styles and events) without having to muck around in wordy tutorials embedded in the code.

If someone were to submit patches that add descriptions like "This
feature is not available on Win32" to the SWT Javadoc, do you think the
SWT developers would accept such a patch? For example, for the
button.setBackground() call, the JavaDoc for the Win32 OS could comment
that this feature does nothing unless you are on WinXP and you have the
theming enabled with a manifest -- in which case it still doesn't set
the background on the button itself, but instead sets the background
around the button if the button is not perfectly rectangular. Such
information would be invaluable.

I think everyone would be more productive if such information were
quickly accessible, and having to search thousands of news messages to
see what questions have been asked isn't very productive! :) I'm not
talking about tutorials, but simple statements in the JavaDoc -- where
appropriate -- that describe decisions made that affect basic function.

Thanks

Eddie
Re: change button's background color [message #467416 is a reply to message #467376] Tue, 31 January 2006 05:28 Go to previous messageGo to next message
Edward Kuns is currently offline Edward KunsFriend
Messages: 14
Registered: July 2009
Junior Member
Kevin Peck wrote:
> Not sure what you are doing with the button. Maybe if we know that we
> can find another solution. If you don't care that the button looks
> exactly like the OS button you can paint your own Canvas with colors,

I am not the original questioner, but I think I am trying to do
something similar. That is, I have been asked to create a program where
the background colors on the buttons are specific R,G,B colors. I have
figured out how to do this with code similar to the code below to make a
button image, and then use setImage() on the button to set the created
image on the button.

In this code, "white" is a global Color equal to RGB(display,
255,255,255), as one would expect. "almostWhite" is a Color equal to
RGB(display, 250,250,250) so it will not interfere with the transparency
detecting code. "font" is a global Font such as "new Font(display,
"Tahoma", 6, SWT.NORMAL). "display" is a global static equal to the
display the Shell is on. The method below is not meant (in this
context) to be called with "background == null" but it allows you to do so.

The button passed in is created with code like:

final Button button = new Button(shell, SWT.PUSH);
button.setFont(font);
button.setSize(70,22);
button.setLocation(x, y);
button.setForeground(almostWhite);
Image imgUp = makeButtonImage(text, button, bgColor1, 0);
Image imgDn = makeButtonImage(text, button, bgColor2, 0);
button.setImage(imgUp);
// Code to add mouse listeners to set the image after mouse presses

And the images are made with the code:

private static Image makeButtonImage(String text, Button button,
Color background, int fuzz) {
Display display = button.getDisplay();
Rectangle bounds = button.getBounds();
int width = bounds.width - fuzz;
int height = bounds.height - fuzz;

// Get the size of the text
GC gc = new GC(button);
Point textSize = gc.textExtent(text, SWT.DRAW_MNEMONIC);
gc.dispose();

// Create the requested image
Image image = new Image(display, width, height);
gc = new GC(image);
gc.setBackground(background == null ? white : background);
gc.setForeground(button.getForeground());
gc.fillRectangle(0, 0, width, height);
gc.setFont(button.getFont());
gc.drawText(text, (width - textSize.x)/2, (height - textSize.y)/2,
true);
gc.dispose();

return image;
}

This code works fine when the button is rectangular, but on a themed
image, it overwrites the whole button including the OS parts of the
button. So far, I have not discovered a way to get the "inside" area of
the button. If I make: GC gc = new GC(button); and ask that GC for
the clipping -- either Rectangle or Region -- I get an empty clipping
region. I cannot find a way to work with the OS button with the
exception of the color of the button background. That's why I added the
"fuzz" parameter to the method above.

The ideal would be a way to get a clipping that, if used, would populate
*only* the inside of the button.

I use the code above to create two images with different background
colors. Then I add MouseUp and MouseDown listeners. The MouseDown
listener always sets the "down" image. The MouseUp listener sets the up
or down image depending on the value of button.getSelection(). This
works perfectly as long as I am happy with rectangular buttons, but as
soon as I want themed button SHAPES but custom colors, it falls apart.

My answer here might actually help the original questioner, but is there
any way to create an image that will ONLY fill the actual interior of a
button (or other widget)?

Thanks

Eddie
Re: change button's background color [message #467458 is a reply to message #467414] Tue, 31 January 2006 17:41 Go to previous message
Daniel Spiewak is currently offline Daniel SpiewakFriend
Messages: 263
Registered: July 2009
Senior Member
<i>If someone were to submit patches that add descriptions like "This
feature is not available on Win32" to the SWT Javadoc, do you think the
SWT developers would accept such a patch?</i>

I honestly don't know. My best suggestions would be to a) start a new thread here and post your javadoc patches and hope that Steve, Veronika, Grant, or someone big sees it. You could b) open an enhancement request against SWT in bugzilla; this is probably the best way to draw attention to it. Or you could choose c) and email one of the big dogs directly; I don't really recomend this since (for some reason) they don't like getting personal email about their corporate projects. :-)
Previous Topic:[FormToolkit] Defining TableWrapData width
Next Topic:Long running wizard process
Goto Forum:
  


Current Time: Thu Apr 25 14:09:13 GMT 2024

Powered by FUDForum. Page generated in 0.03594 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top