JProgressbar update problem ?
Hi there,
I wrote a GUI for a P2P app and I want to display the downloading status (% of received file) with a JProgressbar :
Here is my code:
public class GUI extends JFrame {
private GUIPCProtocol peerClient = null;
private JButton jButton1;
private static JButton jButton2;
private static JButton jButton3;
private static JButton jButton4;
private static JButton jButton5;
private static JButton jButton6;
private static JButton jButton7;
private JButton jButtonmenu;
private JPanel contentPane;
private JLabel jLabel1;
private JLabel jLabel2;
protected static JLabel jLabel3;
private JLabel jLabela;
private JLabel jLabelb;
private JLabel jLabelc;
private JLabel jLabeld;
private JLabel jLabele;
private JLabel jLabelf;
private JLabel jLabelg;
private static JTextArea informations;
private JScrollPane jScrollPane4;
private static JPanel jPanel1; //Panel (Info Downloaded Files)
private static JProgressBar progressBar;
private JPanel jPanel2;//Menu principal
private JPanel jPanel5; //status bar
private JPanel menu;
private GUIInformations infos;
private GUISearch search;
private GUIRegister register;
private GUIDownloadEntire downloadentire;
private GUIDownloadFractionned downloadfracionned;
private GUIDownloadEncoded downloadencoded;
private GUIDownloadEncrypted downloadencrypted;
public GUI(String id)
{
super();
peerClient = new GUIPCProtocol (id);
infos = new GUIInformations(peerClient);
search = new GUISearch(peerClient);
register = new GUIRegister(peerClient);
downloadentire = new GUIDownloadEntire(peerClient);
downloadfracionned = new GUIDownloadFractionned(peerClient);
downloadencoded = new GUIDownloadEncoded(peerClient);
downloadencrypted = new GUIDownloadEncrypted(peerClient);
initializeComponent();
this.setVisible(true);
}
private void initializeComponent() {
jButton1 = new JButton();
jButton2 = new JButton();
jButton3 = new JButton();
jButton4 = new JButton();
jButton5 = new JButton();
jButton6 = new JButton();
jButton7 = new JButton();
jButtonmenu = new JButton();
contentPane = (JPanel)this.getContentPane();
//--
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jLabela = new JLabel();
jLabelb = new JLabel();
jLabelc = new JLabel();
jLabeld = new JLabel();
jLabele = new JLabel();
jLabelf = new JLabel();
jLabelg = new JLabel();
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true);
informations = new JTextArea();
jScrollPane4 = new JScrollPane();
jPanel1 = new JPanel();
//--
jPanel2 = new JPanel ();
//--
jPanel5 = new JPanel();
//--
//jButton1.setText("REGISTER");
jButton1.setHorizontalTextPosition(SwingConstants.CENTER);
jButton1.setIcon(new ImageIcon("images\\register.png"));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
//jButton2.setText("INFOS");
jButton2.setHorizontalTextPosition(SwingConstants.CENTER);
jButton2.setIcon(new ImageIcon("images\\infos.png"));
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton2_actionPerformed(e);
}
});
//jButton3.setText("SEARCH");
jButton3.setHorizontalTextPosition(SwingConstants.CENTER);
jButton3.setIcon(new ImageIcon("images\\search.png"));
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton3_actionPerformed(e);
}
});
//jButton4.setText("ENTIRE");
jButton4.setHorizontalTextPosition(SwingConstants.CENTER);
jButton4.setIcon(new ImageIcon("images\\download_entire.png"));
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton4_actionPerformed(e);
}
});
//jButton5.setText("PARTED");
jButton5.setHorizontalTextPosition(SwingConstants.CENTER);
jButton5.setIcon(new ImageIcon("images\\download_fractionned.png"));
jButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton5_actionPerformed(e);
}
});
//jButton6.setText("ENCODED");
jButton6.setHorizontalTextPosition(SwingConstants.CENTER);
jButton6.setIcon(new ImageIcon("images\\download_encoded.png"));
jButton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton6_actionPerformed(e);
}
});
//jButton7.setText("ENCRYPTED");
jButton7.setHorizontalTextPosition(SwingConstants.CENTER);
jButton7.setIcon(new ImageIcon("images\\download_encrypted.png"));
jButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton7_actionPerformed(e);
}
});
jButtonmenu.setText("Menu");
jButtonmenu.setHorizontalTextPosition(SwingConstants.CENTER);
jButtonmenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButtonmenu_actionPerformed(e);
}
});
contentPane.setLayout(null);
addComponent(contentPane, jPanel1, 550,0,250,550);
addComponent(contentPane, jPanel2, 0,0,550,550);
addComponent(contentPane, jPanel5, 0,550,800,18);
jLabel1.setText("P2P GUI Menu");
Font curFont = jLabel1.getFont();
Font font3 = new Font(curFont.getFontName(),curFont.getStyle(),32);
jLabel1.setFont(font3);
jLabel2.setText("Downloaded files :");
jLabela.setText("Register");
jLabelb.setText("Infos");
jLabelc.setText("Search");
jLabeld.setText("Download Entire");
jLabele.setText("Download Parted");
jLabelf.setText("Download Encoded");
jLabelg.setText("Download Encrypted");
jLabela.setHorizontalAlignment(SwingConstants.CENTER);
jLabelb.setHorizontalAlignment(SwingConstants.CENTER);
jLabelc.setHorizontalAlignment(SwingConstants.CENTER);
jLabeld.setHorizontalAlignment(SwingConstants.CENTER);
jLabele.setHorizontalAlignment(SwingConstants.CENTER);
jLabelf.setHorizontalAlignment(SwingConstants.CENTER);
jLabelg.setHorizontalAlignment(SwingConstants.CENTER);
jScrollPane4.setViewportView(informations);
jScrollPane4.setPreferredSize(new Dimension(200, 300));
//
// jPanel1
//
jPanel1.setLayout(null);
addComponent(jPanel1, jButtonmenu, 40, 20, 60, 25);
addComponent(jPanel1, jLabel2, 27,93,95,18);
addComponent(jPanel1, jScrollPane4, 28,118,200,305);
addComponent(jPanel1, progressBar, 30, 450, 200, 30);
//
// jPanel2
//
jPanel2.setLayout(null);
addComponent(jPanel2, jLabel1, 30, 60, 300, 40);
addComponent(jPanel2, jButton1, 30,150,100,100);
addComponent(jPanel2, jLabela, 30, 250, 100, 20);
addComponent(jPanel2, jButton2, 160,150,100,100);
addComponent(jPanel2, jLabelb, 160,250, 100, 20);
addComponent(jPanel2, jButton3, 290,150,100,100);
addComponent(jPanel2, jLabelc, 290,250, 100, 20);
addComponent(jPanel2, jButton4, 30,300,100,100);
addComponent(jPanel2, jLabeld, 30,400, 100, 20);
addComponent(jPanel2, jButton5, 160,300,100,100);
addComponent(jPanel2, jLabele, 160,400, 100, 20);
addComponent(jPanel2, jButton6, 290,300,100,100);
addComponent(jPanel2, jLabelf, 290,400, 100, 20);
addComponent(jPanel2, jButton7, 420,300,100,100);
addComponent(jPanel2, jLabelg, 420,400, 100, 20);
jPanel5.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
jLabel3.setText("Status : NOT CONNECTED");
jLabel3.setForeground(Color.red);
activatebutton(false);
addComponent(jPanel5, jLabel3, 0, 0, 200, 20);
//
// GUIMenu
//
this.setTitle("P2P - Menu");
this.setLocation(new Point(56, 50));
this.setSize(new Dimension(814, 598));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
this.menu = jPanel2;
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height){
c.setBounds(x,y,width,height);
container.add(c);
}
private void jButton1_actionPerformed(ActionEvent e){
remove(jPanel2);
this.jPanel2 = register.paint(peerClient);
addComponent(contentPane, this.jPanel2, 0, 0, 550, 550);
repaint();
}
private void jButton2_actionPerformed(ActionEvent e){
remove(jPanel2);
this.jPanel2 = infos.paint();
addComponent(contentPane, this.jPanel2, 0, 0, 550, 550);
repaint();
}
private void jButton3_actionPerformed(ActionEvent e){
remove(jPanel2);
this.jPanel2 = search.paint();
addComponent(contentPane, this.jPanel2, 0, 0, 550, 550);
this.search.refreshPSlist();
repaint();
}
private void jButton4_actionPerformed(ActionEvent e){
remove(jPanel2);
this.jPanel2 = downloadentire.paint();
addComponent(contentPane, this.jPanel2, 0, 0, 550, 550);
this.downloadentire.refreshPSlist();
repaint();
}
private void jButton5_actionPerformed(ActionEvent e){
remove(jPanel2);
this.jPanel2 = downloadfracionned.paint();
addComponent(contentPane, this.jPanel2, 0, 0, 550, 550);
this.downloadfracionned.refreshPSlist();
repaint();
}
private void jButton6_actionPerformed(ActionEvent e){
remove(jPanel2);
this.jPanel2 = downloadencoded.paint();
addComponent(contentPane, this.jPanel2, 0, 0, 550, 550);
this.downloadencoded.refreshPSlist();
repaint();
}
private void jButton7_actionPerformed(ActionEvent e){
remove(jPanel2);
this.jPanel2 = downloadencrypted.paint();
addComponent(contentPane, this.jPanel2, 0, 0, 550, 550);
this.downloadencrypted.refreshPSlist();
repaint();
}
private void jButtonmenu_actionPerformed(ActionEvent e){
remove(jPanel2);
this.jPanel2 = menu;
addComponent(contentPane, this.jPanel2, 0, 0, 550, 550);
repaint();
}
public static void activatebutton(Boolean status){
jButton2.setEnabled(status);
jButton3.setEnabled(status);
jButton4.setEnabled(status);
jButton5.setEnabled(status);
jButton6.setEnabled(status);
jButton7.setEnabled(status);
}
public static void refreshinformations(String news){
String infos = informations.getText();
infos+=news;
informations.setText(infos);
}
public static void refreshprogression(int i){
progressBar.setValue(i);
progressBar.repaint();
}
//============================= Testing ================================//
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new GUI("Georges");
}
}
I call the methode to refresh the Jprogressbar's status from an other class through the public static method refreshprogression(int i); to set the % of my JProgress bar. The code look like this :
...
for (int i = 0; i <= pieces; i ++) {
GUI.refreshprogression(i);
int fileSize = connectionPS.receiveInt();
downloadedFile = connectionPS.receiveBytes(fileSize);
dataOutput.write(downloadedFile);
}
...
THE PROBLEM:
MY JProgressbar isn't updating during the receiving of my data ! It keeps showing 0% and then (when the file is full received) it jumps till 100% !
I've tried to write and other code that isn't related with my P2P system to set the % value of my JProgressbar but it's the same !
Please HELP

