1,105,320 Community Members

how to change color progressbar

Member Avatar
jemz
Practically a Posting Shark
849 posts since Jan 2010
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi can you help me please how can i change the color of progressbar or just look like in windows xp progress bar...please help me...thank you in advance hoping for your positive responds...

Member Avatar
peter_budo
Code tags enforcer
7,933 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

never try it before, but apparently changing properties of UIManager before creating progress bar should work. Something like this

UIManager.put("ProgressBar.selectionBackground",Color.BLUE);
UIManager.put("ProgressBar.selectionForeground",Color.WHITE);
JProgressBar progressBar.....
Member Avatar
jemz
Practically a Posting Shark
849 posts since Jan 2010
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

never try it before, but apparently changing properties of UIManager before creating progress bar should work. Something like this

UIManager.put("ProgressBar.selectionBackground",Color.BLUE);
UIManager.put("ProgressBar.selectionForeground",Color.WHITE);
JProgressBar progressBar.....

hi sir thank you for the reply i will try this sir and i will write again if get trouble..more power to you...

Member Avatar
jemz
Practically a Posting Shark
849 posts since Jan 2010
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

never try it before, but apparently changing properties of UIManager before creating progress bar should work. Something like this

UIManager.put("ProgressBar.selectionBackground",Color.BLUE);
UIManager.put("ProgressBar.selectionForeground",Color.WHITE);
JProgressBar progressBar.....

sir please help me on this it will not change the color...please correct me sir...thank you in advance hoping for your positive responds...

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

public class Progress extends JFrame {

    JProgressBar current;
    JTextArea out;
    JButton find;
    Thread runner;
    int num = 0;

  
    public Progress() {
        super("Progress");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
        JPanel pane = new JPanel();
      
        UIManager.put("ProgressBar.selectionBackground",Color.BLUE);
        UIManager.put("Progress.selectionForeground",Color.GREEN);
        pane.setLayout(new FlowLayout());
        current = new JProgressBar(0, 2000);
        
        
        
        current.setValue(0);
        current.setStringPainted(true);
        pane.add(current);
        setContentPane(pane);
        
    }


    public void iterate() {
        while (num < 2000) {
            current.setValue(num);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) { }
            num += 95;
        }
    }

    public static void main(String[] arguments) {
       
        Progress frame = new Progress();
        frame.pack();
        frame.setVisible(true);
        frame.iterate();
      
      
   }
}
Member Avatar
Ranjitdani
Newbie Poster
6 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

As you named the JprogressBar as current
Change the following in code also,Hope it works!

UIManager.put("current.selectionBackground",Color.BLUE);
UIManager.put("current.selectionForeground",Color.GREEN);

Member Avatar
peter_budo
Code tags enforcer
7,933 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

As you named the JprogressBar as current
Change the following in code also,Hope it works!

UIManager.put("current.selectionBackground",Color.BLUE);
UIManager.put("current.selectionForeground",Color.GREEN);

Nah that wouldn't.
I will give it one more shot, I will look up source code for one of the L&F projects I know and see if I find something there

Member Avatar
peter_budo
Code tags enforcer
7,933 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
1
 

OK here is solution, enjoy it!

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JProgressBarDemo extends JFrame {

  protected int minValue = 0;

  protected int maxValue = 100;

  protected int counter = 0;

  protected JProgressBar progressBar;

  public JProgressBarDemo() {
    super("JProgressBar Demo");
    setSize(300, 100);

    UIManager.put("ProgressBar.background", Color.BLACK); //colour of the background
    UIManager.put("ProgressBar.foreground", Color.RED);  //colour of progress bar
    UIManager.put("ProgressBar.selectionBackground",Color.YELLOW);  //colour of percentage counter on black background
    UIManager.put("ProgressBar.selectionForeground",Color.BLUE);  //colour of precentage counter on red background


    progressBar = new JProgressBar();
    progressBar.setMinimum(minValue);
    progressBar.setMaximum(maxValue);
    progressBar.setStringPainted(true);

    JButton start = new JButton("Start");
    start.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Thread runner = new Thread() {
          public void run() {
            counter = minValue;
            while (counter <= maxValue) {
              Runnable runme = new Runnable() {
                public void run() {
                  progressBar.setValue(counter);
                }
              };
              SwingUtilities.invokeLater(runme);
              counter++;
              try {
                Thread.sleep(100);
              } catch (Exception ex) {
              }
            }
          }
        };
        runner.start();
      }
    });

    getContentPane().add(progressBar, BorderLayout.CENTER);
    getContentPane().add(start, BorderLayout.WEST);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    addWindowListener(wndCloser);
    setVisible(true);
  }

  public static void main(String[] args) {
    new JProgressBarDemo();
  }
}
Member Avatar
jemz
Practically a Posting Shark
849 posts since Jan 2010
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

OK here is solution, enjoy it!

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JProgressBarDemo extends JFrame {

  protected int minValue = 0;

  protected int maxValue = 100;

  protected int counter = 0;

  protected JProgressBar progressBar;

  public JProgressBarDemo() {
    super("JProgressBar Demo");
    setSize(300, 100);

    UIManager.put("ProgressBar.background", Color.BLACK); //colour of the background
    UIManager.put("ProgressBar.foreground", Color.RED);  //colour of progress bar
    UIManager.put("ProgressBar.selectionBackground",Color.YELLOW);  //colour of percentage counter on black background
    UIManager.put("ProgressBar.selectionForeground",Color.BLUE);  //colour of precentage counter on red background


    progressBar = new JProgressBar();
    progressBar.setMinimum(minValue);
    progressBar.setMaximum(maxValue);
    progressBar.setStringPainted(true);

    JButton start = new JButton("Start");
    start.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Thread runner = new Thread() {
          public void run() {
            counter = minValue;
            while (counter <= maxValue) {
              Runnable runme = new Runnable() {
                public void run() {
                  progressBar.setValue(counter);
                }
              };
              SwingUtilities.invokeLater(runme);
              counter++;
              try {
                Thread.sleep(100);
              } catch (Exception ex) {
              }
            }
          }
        };
        runner.start();
      }
    });

    getContentPane().add(progressBar, BorderLayout.CENTER);
    getContentPane().add(start, BorderLayout.WEST);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    addWindowListener(wndCloser);
    setVisible(true);
  }

  public static void main(String[] args) {
    new JProgressBarDemo();
  }
}

hello sir thank you for the reply helping me... i will try this sir and i will write again if i have doubt...more power to you sir...thanks...

Question Answered as of 3 Years Ago by peter_budo and Ranjitdani
Samanta Goutam
Newbie Poster
1 post since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

Thank you sir peter budo

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: