changing the color of JProgressBar

Discussion in 'Java' started by Aryeh M. Friedman, Dec 20, 2007.

  1. I have a JProgressBar that I want to be green if the underlaying
    process has no errors and red if it does. I can't seem to find a way
    of setting the color of the bar in the raw JProgressBar class... ideas?
    Aryeh M. Friedman, Dec 20, 2007
    #1
    1. Advertising

  2. Aryeh M. Friedman

    HightowerC Guest

    On Dec 20, 3:12 pm, "Aryeh M. Friedman" <>
    wrote:
    > I have a JProgressBar that I want to be green if the underlaying
    > process has no errors and red if it does. I can't seem to find a way
    > of setting the color of the bar in the raw JProgressBar class... ideas?


    Try JProgressBar.setForeground(Color c)

    HightowerC
    HightowerC, Dec 20, 2007
    #2
    1. Advertising

  3. On Dec 20, 4:53 pm, HightowerC <> wrote:
    > On Dec 20, 3:12 pm, "Aryeh M. Friedman" <>
    > wrote:
    >
    > > I have a JProgressBar that I want to be green if the underlaying
    > > process has no errors and red if it does. I can't seem to find a way
    > > of setting the color of the bar in the raw JProgressBar class... ideas?

    >
    > Try JProgressBar.setForeground(Color c)


    No effect
    Aryeh M. Friedman, Dec 21, 2007
    #3
  4. Aryeh M. Friedman

    Qu0ll Guest

    "Aryeh M. Friedman" <> wrote in message
    news:...

    >I have a JProgressBar that I want to be green if the underlaying
    > process has no errors and red if it does. I can't seem to find a way
    > of setting the color of the bar in the raw JProgressBar class... ideas?


    I think you'll find the colour is controlled by the prevailing PLAF.

    --
    And loving it,

    -Q
    _________________________________________________

    (Replace the "SixFour" with numbers to email me)
    Qu0ll, Dec 21, 2007
    #4
  5. Aryeh M. Friedman

    Eric Sosman Guest

    Aryeh M. Friedman wrote:
    > On Dec 20, 4:53 pm, HightowerC <> wrote:
    >> On Dec 20, 3:12 pm, "Aryeh M. Friedman" <>
    >> wrote:
    >>
    >>> I have a JProgressBar that I want to be green if the underlaying
    >>> process has no errors and red if it does. I can't seem to find a way
    >>> of setting the color of the bar in the raw JProgressBar class... ideas?

    >> Try JProgressBar.setForeground(Color c)

    >
    > No effect


    Works for me.


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    class Bar extends JFrame {

    public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Bar().setVisible(true);
    }
    });
    }

    private final JProgressBar pbar = new JProgressBar(0, 10);
    private final JButton butt = new JButton("Click Me");

    private Bar() {
    butt.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    BoundedRangeModel model = pbar.getModel();
    int value = model.getValue();
    if (value == 3)
    pbar.setForeground(Color.GREEN);
    else if (value == 7)
    pbar.setForeground(Color.RED);
    model.setValue(value + 1);
    }
    });

    setLayout(new FlowLayout());
    add(pbar);
    add(butt);
    pack();
    }
    }

    --
    Eric Sosman, Dec 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cengiz

    Update of JProgressBar

    Cengiz, Aug 6, 2003, in forum: Java
    Replies:
    2
    Views:
    882
    Cengiz
    Aug 7, 2003
  2. Inertia_sublimation
    Replies:
    5
    Views:
    2,115
    Inertia_sublimation
    Jan 3, 2004
  3. FET

    JProgressbar look n feel

    FET, Jul 22, 2004, in forum: Java
    Replies:
    3
    Views:
    1,493
  4. Sebastian Millies

    Color-coded JProgressBar?

    Sebastian Millies, Jan 12, 2007, in forum: Java
    Replies:
    0
    Views:
    455
    Sebastian Millies
    Jan 12, 2007
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    411
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page