473,466 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

JButton Hellp

54 New Member
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
12 3621
JosAH
11,448 Recognized Expert MVP
@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
JosAH
11,448 Recognized Expert MVP
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
Humakt
42 New Member
@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
yeshello54
54 New Member
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
JosAH
11,448 Recognized Expert MVP
@Humakt
That is just rubbish.

kind regards,

Jos
Jul 24 '09 #6
JosAH
11,448 Recognized Expert MVP
@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
Humakt
42 New Member
@JosAH
Think what you will but it works.
Jul 24 '09 #8
JosAH
11,448 Recognized Expert MVP
@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
Humakt
42 New Member
@JosAH
It's not magic: "this" is referencing to the JButton being created of course.
Jul 24 '09 #10
JosAH
11,448 Recognized Expert MVP
@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
Humakt
42 New Member
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
JosAH
11,448 Recognized Expert MVP
@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

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

Similar topics

0
by: Jeff T. | last post by:
Hi, I have a JMenuBar object that contains a JToolBar object which contains some JButton objects. After I let my applet sit for > 20 minutes, the graphics on those buttons would disappear. ...
3
by: Konrad Den Ende | last post by:
Is it possible to do at all? The software will be used on computers with western operative systems (so i guess, JVM will be english) but i'm asured that japanese will be enabled through IME so that...
4
by: porky008 | last post by:
I put in a next and previous button but it crashes when I run it. I also need it to loop around to the first or last if at the ends. Can some one help me with this? import java.awt.*; import...
0
by: Oswald Otte | last post by:
Hello How can I change the look of a disabled JButton? I have made a boardgame with a brown background and brown buttons. I have also put an imageicon on the buttons that has a colored image on...
9
by: SimonHeffer | last post by:
I've been asked to give my .NET app the same look and feel to another app written using Java/Swing. My main issue is to do with button/icons. The JButton used by the other app has an icon with a...
6
by: vijaykumarsharma | last post by:
Hi all I am having a problem with JButton. JButton b=new JButton("save"); how can i get the name of the JButton b. Please give the anser for me
2
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: mlibot | last post by:
My JButton will be disabled when running my program... my JButton will be enabled if the value or parameter are equal to 3... for example: 1 value is path of the file i selected. 2 vaue is path of...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.