By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

JButton Hellp

P: 54
So I want to set the background color of a Jbutton. I know that the normal way to do such a thing is down the following way:

JButton button = new JButton("xxxx");
button.setBackground(Color.RED);

but in my program i add my Jbutton the following way..

horizontalBox2=Box.createHorizontalBox();
horizontalBox2.add(new JButton("Black"));

so how would i go about setting the background color of this button to say black??? Any help would be appreciated. Thanks.
Jul 24 '09 #1
Share this Question
Share on Google+
12 Replies


Expert 10K+
P: 11,448
@yeshello54
That is just programming: you need to have a reference to that button yourself so you can set its background colour:

Expand|Select|Wrap|Line Numbers
  1.     horizontalBox2=Box.createHorizontalBox();
  2.     JButton button= new JButton("Black");
  3.     button.setBackground(Color.RED);
  4.     horizontalBox2.add(button);
  5.  
You could've and should've known this.

kind regards,

Jos
Jul 24 '09 #2

Expert 10K+
P: 11,448
And please do use those code tags. Text like this:

[code]
your text here ...
[/code]

will display like this:

Expand|Select|Wrap|Line Numbers
  1. your text here ...
  2.  
kind regards,

Jos
Jul 24 '09 #3

P: 42
@JosAH
Actually that was what he was telling. There is another way though:

Expand|Select|Wrap|Line Numbers
  1. horizontalBox2.add(new JButton("Black"){{this.setBackground(Color.RED);}});
I used something like that with JLabels before. Referencing button afterwards is problematic.
Jul 24 '09 #4

P: 54
thanks for that..ya i shouldve know that..and i will use code tags now..also i think what i really wanted is to know if you can actually change the button color itself and not the button background. Thanks.
Jul 24 '09 #5

Expert 10K+
P: 11,448
@Humakt
That is just rubbish.

kind regards,

Jos
Jul 24 '09 #6

Expert 10K+
P: 11,448
@yeshello54
If you want to change the colour of the caption text you should change the foreground colour. If you want to change the colours(s) of the border you should set another border on that button.

kind regards,

Jos
Jul 24 '09 #7

P: 42
@JosAH
Think what you will but it works.
Jul 24 '09 #8

Expert 10K+
P: 11,448
@Humakt
Good; it all depends on the class of 'this' in your example. Don't believe in magic.

kind regards,

Jos
Jul 24 '09 #9

P: 42
@JosAH
It's not magic: "this" is referencing to the JButton being created of course.
Jul 24 '09 #10

Expert 10K+
P: 11,448
@Humakt
I know and I apologize: I missed/mentally added a semi-colon and misread your example. Your example does work.

kind regards,

Jos
Jul 24 '09 #11

P: 42
I accept your apology.

I don't use this kind of "inner" class often but it is handy sometimes if you want to create lots of objects and store them at collection.

Example:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class CreateOnFlight {
  3.     private int number;
  4.     private String text1;
  5.     public void setText(String a){
  6.         text1 = a;
  7.     }
  8.     public void setInt(int p){
  9.         number = p;
  10.     }
  11.     public int getInt(){
  12.         return number;
  13.     }
  14.     public String getString(){
  15.         return text1;
  16.     }
  17.     public CreateOnFlight(){}
  18.     public static void main(String[] args){
  19.         Vector<CreateOnFlight> ll = new Vector<CreateOnFlight>();
  20.         for(int i = 0; i<100; i++){
  21.             final int a = i;
  22.             ll.add(new CreateOnFlight(){{
  23.                 this.setInt(a);
  24.                 this.setText("aa"+a);
  25.             }
  26.             });
  27.         }
  28.         for(int i = 0; i<ll.size(); i++){
  29.             System.out.println(ll.elementAt(i).getString() + " " + ll.elementAt(i).getInt());
  30.         }
  31.     }
  32. }
Jul 24 '09 #12

Expert 10K+
P: 11,448
@Humakt
r035198x even wrote a little article about this very issue. Indeed, this little trick is often overlooked (even by me: I simply misread the code ;-)

kind regards,

Jos
Jul 24 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.