23 May 2011

JProgressBar Tutorial


JProgressBar Tutorial
Hello Friends,

Nice to see you again.
Today we will learn JProgressBar in Java Swing.

                  JProgressBar is very important Swing Component.We can use it in our 
Swing Application when we have to wait User For some Process To be competed.
In these type of situation , we can display JProgressBar in JFrame.
                  So be ready to learn this simple exmaple of JProgressBar .After apply 
this code in action, you will be capable of using JProgressBar in your 
Swing Applications.

                 This simple example of JProgressBar  gives you the basic idea of 
JProgressBar and you will be familiar for using JProgressBar .

Output will be like this:








OK For this you have to create one class called PaintedProgressBarDemo.java and paste the below code in it.
And run it....Simple right..?


-------------------------------------------------------------------------------------------

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;


public class PaintedProgressBarDemo extends JFrame{
final JTextArea textArea = new JTextArea();
final JProgressBar progress = new JProgressBar();

public PaintedProgressBarDemo() {
super("Painted Progress Bar Demo");
setLayout(new GridBagLayout());
GridBagConstraints cn = new GridBagConstraints();
cn.insets = new Insets(5, 5, 5, 5);
cn.gridx = 0;
cn.gridy = 0;

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());

Font font=new Font("", Font.BOLD, 14);

textArea.setPreferredSize(new Dimension(500, 150));
textArea.setBackground(Color.pink);
textArea.setBorder(BorderFactory.createBevelBorder(1));
// Here We apply font from normal to bold and
// increase size to 14
textArea.setFont(font);
panel.add(textArea, BorderLayout.NORTH);

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);

progress.setMaximum(100);
progress.setStringPainted(true);
progress.setBackground(Color.white);
progress.setForeground(Color.blue);
progress.setPreferredSize(new Dimension(150, 20));
progress.setVisible(true);
progress.setValue(0);
panel.add(progress, BorderLayout.SOUTH);

add(panel, cn);
pack();

//Setting JTextArea Cursor In Wait Condition To
// Complete The Process
textArea.setCursor(Cursor.getPredefinedCursor
(Cursor.WAIT_CURSOR));
perform();

//Setting JTextArea Cursor In Default Condition
textArea.setCursor(Cursor.getPredefinedCursor
(Cursor.DEFAULT_CURSOR));
}

public void iterate() {
int num = 40;
while (num <= 100) {

progress.setValue(num);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {

}
num += 20;
}
}

public void perform() {
String data = "Nice To See You....This is Painted" +
" JProgressBar Demo.!";
try {
Thread.sleep(2000);
iterate();

textArea.setText(data);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void main(String s[]) {

PaintedProgressBarDemo dd=new PaintedProgressBarDemo();
}

}
--------------------------------------------------------------------

So I hope this code is helpful in understanding the working of JProgressBar.


Feel free to ask me if you have any dought regarding this.
You can also give me suggessions to improve this tutorial.


Leave comments if you like this post...!


Thank you.
Nirav Raval


----------------------------------------------------------------------------------
Follow Other Java Swing Tricks....

No comments:

Post a Comment

Leave Comment If You Like This...Thank You..!