JButton Hellp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yeshello54
    New Member
    • Mar 2009
    • 54

    JButton Hellp

    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.setBackg round(Color.RED );

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

    horizontalBox2= Box.createHoriz ontalBox();
    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.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by yeshello54
    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.setBackg round(Color.RED );

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

    horizontalBox2= Box.createHoriz ontalBox();
    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.
    That is just programming: you need to have a reference to that button yourself so you can set its background colour:

    Code:
        horizontalBox2=Box.createHorizontalBox();
        JButton button= new JButton("Black");
        button.setBackground(Color.RED);
        horizontalBox2.add(button);
    You could've and should've known this.

    kind regards,

    Jos

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      And please do use those code tags. Text like this:

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

      will display like this:

      Code:
      your text here ...
      kind regards,

      Jos

      Comment

      • Humakt
        New Member
        • Aug 2008
        • 42

        #4
        Originally posted by JosAH
        You could've and should've known this.

        kind regards,

        Jos
        Actually that was what he was telling. There is another way though:

        Code:
        horizontalBox2.add(new JButton("Black"){{this.setBackground(Color.RED);}});
        I used something like that with JLabels before. Referencing button afterwards is problematic.

        Comment

        • yeshello54
          New Member
          • Mar 2009
          • 54

          #5
          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.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Humakt
            Actually that was what he was telling. There is another way though:

            Code:
            horizontalBox2.add(new JButton("Black"){{this.setBackground(Color.RED);}});
            I used something like that with JLabels that were generated in loop before. Referencing button afterwards is problematic.
            That is just rubbish.

            kind regards,

            Jos

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by yeshello54
              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.
              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

              Comment

              • Humakt
                New Member
                • Aug 2008
                • 42

                #8
                Originally posted by JosAH
                That is just rubbish.
                Think what you will but it works.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Humakt
                  Think what you will but it works.
                  Good; it all depends on the class of 'this' in your example. Don't believe in magic.

                  kind regards,

                  Jos

                  Comment

                  • Humakt
                    New Member
                    • Aug 2008
                    • 42

                    #10
                    Originally posted by JosAH
                    Good; it all depends on the class of 'this' in your example. Don't believe in magic.

                    kind regards,

                    Jos
                    It's not magic: "this" is referencing to the JButton being created of course.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by Humakt
                      It's not magic: "this" is referencing to the JButton being created of course.
                      I know and I apologize: I missed/mentally added a semi-colon and misread your example. Your example does work.

                      kind regards,

                      Jos

                      Comment

                      • Humakt
                        New Member
                        • Aug 2008
                        • 42

                        #12
                        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:

                        Code:
                        import java.util.*;
                        public class CreateOnFlight {
                        	private int number;
                        	private String text1;
                        	public void setText(String a){
                        		text1 = a;
                        	}
                        	public void setInt(int p){
                        		number = p;
                        	}
                        	public int getInt(){
                        		return number;
                        	}
                        	public String getString(){
                        		return text1;
                        	}
                        	public CreateOnFlight(){}
                        	public static void main(String[] args){
                        		Vector<CreateOnFlight> ll = new Vector<CreateOnFlight>();
                        		for(int i = 0; i<100; i++){
                        			final int a = i;
                        			ll.add(new CreateOnFlight(){{
                        				this.setInt(a);
                        				this.setText("aa"+a);
                        			}
                        			});
                        		}
                        		for(int i = 0; i<ll.size(); i++){
                        			System.out.println(ll.elementAt(i).getString() + " " + ll.elementAt(i).getInt());
                        		}
                        	}
                        }

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by Humakt
                          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.
                          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

                          Comment

                          Working...