Progressbar demo: set selection background, selection foreground and foreground : ProgressBar « Swing JFC « Java






Progressbar demo: set selection background, selection foreground and foreground

Progressbar demo: set selection background, selection foreground and foreground
  

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.selectionBackground", Color.black);
    UIManager.put("ProgressBar.selectionForeground", Color.white);
    UIManager.put("ProgressBar.foreground", new Color(8, 32, 128));

    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();
  }
}
           
         
    
  








Related examples in the same category

1.Create a ProgressBar
2.Create a horizontal progress bar
3.Create a vertical progress bar
4.Creating a JProgressBar Component with an Unknown Maximum
5.Set all the values at once by using the model
6.Listening for Value Changes in a JProgressBar Component
7.Displaying the Percentage Done on a JProgressBar Component
8.Getting and Setting the Values of a JProgressBar Component
9.ProgressBar Demo: long taskProgressBar Demo: long task
10.When information on the task's progress is available, the progress bar can be made determinate:
11.Implement a progressbar in your application
12.ProgressMonitor DemoProgressMonitor Demo
13.Creating a modal progress dialog
14.ProgressBar Demo 2ProgressBar Demo 2
15.Use an actual input file to monitor rather than inducing progress manually
16.A demonstration of the ProgressMonitor toolbarA demonstration of the ProgressMonitor toolbar
17.A demonstration of the JProgressBar componentA demonstration of the JProgressBar component
18.Progress bar SampleProgress bar Sample
19.Sample ProgressSample Progress
20.ProgressBar StepProgressBar Step
21.ProgressBar DemoProgressBar Demo
22.This program demonstrates the use of a progress bar to monitor the progress of a thread.This program demonstrates the use of a progress bar to monitor the progress of a thread.