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 3 of 3

Thread: How do i make my Jbutton ( with icon ) keep his collors when i disable him?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do i make my Jbutton ( with icon ) keep his collors when i disable him?

    I made a Jbutton:

    private JButton button1 = new JButton();

    then my icon

    ImageIcon icon = new ImageIcon("imagepath");

    button1.setIcon(icon)

    and later in the program I disable the button ( its a tic tac toe game ), but the icon also turns gray ( it is supossed to stay a red square ), and idk how to fix that.


    ( my first time posting, idk if i did ok, please post tips )


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do i make my Jbutton ( with icon ) keep his collors when i disable him?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    You did fine on your first post. One or two lines of code outside code tags is fine, but more than that (roughly) should be posted inside tags.

    Two options come immediate to mind:

    1. Rewrite the default JButton behavior when disabled, or

    2. instead of using the setEnable( false ) method that causes the undesired behavior, you could programmatically cause the button to be unselectable, unchangeable, un-whatever-you-need so that it retains its appearance but is no longer in play.

    Number 2 is probably easier, but I haven't looked into doing number 1.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: How do i make my Jbutton ( with icon ) keep his collors when i disable him?

    Option 3: Use the setDisabledIcon(Icon disabledIcon) method, passing to it the same icon used for button1.setIcon(icon). When the button is disabled, the button will be greyed out but its icon will not.

  4. The Following User Says Thank You to jashburn For This Useful Post:

    GregBrannon (May 25th, 2014)

Similar Threads

  1. How do I make a jTextField display a value when a jButton is pressed?
    By idontknowhowtocode in forum AWT / Java Swing
    Replies: 0
    Last Post: December 12th, 2013, 02:17 PM
  2. how can i make a jbutton to close a jframe??
    By FastFuture in forum AWT / Java Swing
    Replies: 44
    Last Post: June 2nd, 2013, 12:05 AM
  3. How do i make My Jbutton Edit selected Row in Jtable
    By legend101z in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 7th, 2013, 04:59 PM
  4. Make JTextArea accept input from JButton
    By crazed8s in forum AWT / Java Swing
    Replies: 9
    Last Post: December 5th, 2010, 09:38 PM
  5. how to make a simple JButton on a JFrame window?
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: November 20th, 2009, 10:08 PM

Tags for this Thread