Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Changing a JButton background

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Location
    South Africa
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Changing a JButton background

    Hi

    So I'm making a small game in NetBeans. In my GUI I have a jButton and I want to know, is it possible to change the background of the JButton once it has been set? If so, can I change it from a separate method in a different class? If not, what else can I do?

    Thanks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Changing a JButton background

    Changing an object that belongs to another class is usually done with a "setter" or mutator method. In your case, the class that owns the JButton might have a method, public void setButtonBackground( Type background ), that can be called by other classes to change the button's background. If you're not familiar with 'getter' (accessor) and 'setter' (mutator) methods, then you should search for those topics and learn about them.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Location
    South Africa
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Changing a JButton background

    Yeah I know what accessors and mutators are, but I can't find one for the button to set the background.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Changing a JButton background

    Did you check the JButton API page?

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Changing a JButton background

    If you mean to set a picture on jbutton, you can do that with
    button.setIcon(new ImageIcon("full image path here(c:/my/images/bigboom.jpg), or path in working directory(boom.png), image types of jpg, png, gif...")), also there is similiar methods for disabled icon, selected icon, pressed icon, mouse over button icon ...

  6. The Following User Says Thank You to startas For This Useful Post:

    idanbob (September 15th, 2013)

  7. #6
    Junior Member
    Join Date
    Sep 2013
    Location
    South Africa
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Changing a JButton background

    Yeah I figured that out thanks, my whole program is so messed up though i don't know why it builds and compiles perfectly but it just doesn't work

Similar Threads

  1. Replies: 1
    Last Post: August 4th, 2012, 10:02 AM
  2. New Background
    By Tjstretch in forum Totally Off Topic
    Replies: 6
    Last Post: November 2nd, 2011, 11:25 PM
  3. JButton Auto-changing Reference Variable
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 19th, 2011, 10:57 PM
  4. background
    By b109 in forum AWT / Java Swing
    Replies: 0
    Last Post: May 24th, 2010, 06:37 AM
  5. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 24th, 2010, 11:15 PM

Tags for this Thread