Results 1 to 10 of 10
  1. #1
    kevrod79 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default setBackground Color problem

    Dear all,

    New to Java (green and keen). I have a form with two buttons on it. I want it to change the background of the button from its default color to red, wait a second, then change to blue.

    I am using the following code:

    public void actionPerformed(ActionEvent arg0) {

    btnColor.setBackground(Color.RED);
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    btnColor.setBackground(Color.PINK);

    All that happens is it pauses for a second then changes to pink. It never goes red.

    What am I doing wrong?

    Kev

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: setBackground Color problem

    You need to give the JVM's thread back to it so it can change the color. The actionPerformed method is using the JVMs thread that it uses for GUI management. You need to change the color, release the thread by exiting the method and let the JVM use its thread to change the color. Then at a later time you can change the color again and give control back to the GUI thread to allow it to change the color.
    You could use a Timer to wait a short time and to call a method that will make the second color change.

    Your code and the JVM's code take turns using the thread.

  3. #3
    kevrod79 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: setBackground Color problem

    Thanks for that,

    Can you point me in the right direction to solve this please?

    Thanks,

    kevin

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: setBackground Color problem

    Read up on: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing). You can use the SwingWorker as the background Thread. You can "publish" the background color, then sleep() and then publish the second color.

  5. #5
    kevrod79 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: setBackground Color problem

    Thanks for that, still a bit confused, but I will work it out.

    Kev

  6. #6
    kevrod79 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: setBackground Color problem

    Nope, my brain is beginning to hurt. Could someone provide a clue /code example to help me here.

    Thanks in advance,

    Kevin

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: setBackground Color problem

    In the actionPerformed method set the first color and start a Timer to call another actionPerformed method in 1 second and exit the actionPerformed method. When the Timer calls the other actionPerformed method set the color to the second color and exit the method.
    Do a Search here on the forum for code samples using the Timer class

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: setBackground Color problem

    The tutorial has a working example. It shows you how to "flip a coin" and display the results each time a flip is done.

    Your code will be easier because all you do is publish a Color, sleep() and then publish the second color. No need for a loop.

  9. #9
    krupa is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: setBackground Color problem

    if u wish try the following code

    import java.awt.*;
    import java.awt.event.*;
    public class colordemo extends Frame implements ActionListener
    {
    Button b1,b2,b3;
    boolean a= false;
    colordemo(String s)
    {
    super(s);
    setLayout(new FlowLayout());
    b1=new Button("black");
    b2=new Button("pink");
    b3=new Button("blue");
    add(b1);
    add(b2);
    add(b3);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    addWindowListener(new W());
    }
    public void actionPerformed(ActionEvent e)
    {
    b1=(Button)e.getSource();
    String s1=b1.getLabel();
    if(s1.equals("black"))
    {
    setBackground(Color.black);
    }
    b2=(Button)e.getSource();
    String s2=b2.getLabel();
    if(s2.equals("pink"))
    {
    setBackground(Color.pink);
    }
    b3=(Button)e.getSource();
    String s3=b3.getLabel();
    if(s3.equals("blue"))
    {
    setBackground(Color.blue);
    }
    }
    public class W extends WindowAdapter
    {
    public void WindowClosing(WindowEvent we)
    {
    if(a)
    {
    dispose();
    }
    else
    {
    System.exit(0);
    }
    }
    }

    public static void main(String args[])
    {
    colordemo cd = new colordemo("color");
    cd.setSize(300,200);
    cd.setVisible(true);
    }}

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: setBackground Color problem

    Does the code do what you want it to do? If not, please explain.
    Please edit the post and wrap the code in code tags. See:
    BB Code List - Java Programming Forum

Similar Threads

  1. problem using color constructor
    By mike28 in forum Java Applets
    Replies: 9
    Last Post: 10-26-2011, 06:44 AM
  2. SetBackground for JComponent
    By tulasi.neppali in forum AWT / Swing
    Replies: 5
    Last Post: 09-12-2010, 10:14 PM
  3. Color problem
    By dewitrydan in forum New To Java
    Replies: 6
    Last Post: 08-10-2010, 01:38 AM
  4. Problems with setBackground()
    By Oleg in forum New To Java
    Replies: 6
    Last Post: 03-07-2010, 04:59 PM
  5. JButton.setBackground() Problem
    By ellias2007 in forum AWT / Swing
    Replies: 2
    Last Post: 02-24-2010, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •