Sie sind auf Seite 1von 3

Creating Progress Bar Using JProgressBar Class

About Us

Request a Tutorial

Contact Us

22/09/16, 01)37

Advertising

Privacy Policy

Search this website

Home

SQL

Java Swing

GETTING
STARTED

Introduction to
Java Swing

PL/SQL
JSP

Data Warehouse

Data Mining

PHP

Perl

JavaScript

Python

Home / Java Swing / Creating Progress Bar Using JProgressBar Class

Creating Progress Bar Using JProgressBar


Class

Developing
the First
Swing
Application

LAYOUT
MANAGER

Java Swing
Layouts
BorderLayout
FlowLayout
GridLayout
GridBagLayou
t
CardLayout

In this tutorial, we will show you how to use JProgressBar class to


create progress bars in your Java swing application.
A progress bar is a widget that displays progress of
a lengthy task, for instance file download or transfer. To create a
progress bar in swing you use the JProgressbar class. With
JProgressBar you can either create vertical or horizontal progress

BASIC
COMPONENTS

bar. In addition JProgressBar class allows you to create another


kind of progress bar which is known as indeterminate progress

JButton
JToggleButton
JCheckBox
JRadioButton

bar. The indeterminate progress bar is used to display progress


with unknown progress or the progress cannot express by
percentage.
Here are the constructors of JProgressBar class:

http://www.zentut.com/java-swing/jprogressbar/

Pgina 1 de 3

Creating Progress Bar Using JProgressBar Class

JLabel
JTextField
JTextArea

22/09/16, 01)37

Constructors

Meaning

public JProgressBar()

Creates a horizontal progress bar.

public

Creates a progress bar using BoundedRangeMode

JEditorPane

JProgressBar(BoundedRangeModel hold data.

JSpinner

model)

JComboBox

public JProgressBar(int orient)

Creates a progress bar with a given orientation dete

bySwingConstants.VERTICAL or SwingConstants.H

JList

public JProgressBar(int min, int

JPasswordFie
ld

Creates a progress bar with mininum and maximum

max)

JScrollBar

public JProgressBar(int orient, int

Creates a progress bar with mininum and maximum

min, int max)

progress bars orientation.

JProgressBar
JSlider

Example of using JProgressbar


In this example, we will use JProgressBar class to create a

JAVA SWING
DIALOGS

simple progress bar.

Java Swing
Dialog
JColorChoose
r
JFileChooser
JOptionPane
Creating
Login Dialog
Creating
Menu
Popup Menu
ToolBar
Displaying
Data using
JTable

1
2
3
4
5
6
7
8
9
10
11
12
13

package jprogressbardemo;
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String
String[] args) {
final int MAX = 100;
final JFrame frame = new JFrame("JProgress Demo")

http://www.zentut.com/java-swing/jprogressbar/

// creates progress bar


final JProgressBar pb = new JProgressBar();

Pgina 2 de 3

Creating Progress Bar Using JProgressBar Class

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 }

22/09/16, 01)37

pb.setMinimum(0);
pb.setMaximum(MAX);
pb.setStringPainted(true
true);

// add progress bar


frame.setLayout(new
new FlowLayout());
frame.getContentPane().add(pb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
frame.setSize(300, 200);
frame.setVisible(true
true);

// update progressbar
for (int
int i = 0; i <= MAX; i++) {
final int currentValue = i;
try {
SwingUtilities.invokeLater(new
new Runnable()
public void run() {
pb.setValue(currentValue);
}
});
java.lang.Thread.sleep(100);
} catch (InterruptedException e) {
JOptionPane.showMessageDialog(frame, e.getMessage
}
}
}

Previous Tutorial:

Creating Scrollbar Using JScrollBar


Class

Next Tutorial:

Creating Sliders Using JSlider Class

Copyright 2016 by ZenTut Website. All Rights Reserved.

http://www.zentut.com/java-swing/jprogressbar/

Pgina 3 de 3

Das könnte Ihnen auch gefallen