user2829506
user2829506

Reputation: 1

jprogressbar not working properly( may be due to threading)

i have code as follows

 public class panel extends JPanel{
 String sh;
 String su;
 String sp;
 int sp;
 final static int interval = 100;
  int i;
  Timer timer;
 public static JProgressBar pbar;

 public int getsport() {
       return this.sport;
    }
 public String getshost() {
       return this.shost;
    }
 public String getsuser() {
       return this.suser;
    }
 public String getspass() {
       return this.spass;
    }

public panel(){
    Dimension size = getPreferredSize();
    size.width = 300;//694;
    size.height = 200;//600;
    setPreferredSize(size);
    setBorder(BorderFactory.createTitledBorder("Linux Audit"));
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();


    JLabel labelhost = new JLabel("Host    ");
    JLabel labeluser = new JLabel("User name    ");
    JLabel labelpass = new JLabel("Password    ");
    JLabel labelport = new JLabel("Pass   ");
    final JLabel lb = new JLabel();
    pbar = new JProgressBar(0, 100);
    pbar.setValue(0);
    pbar.setStringPainted(true);
    pbar.setVisible(false);
    final JTextField host = new JTextField(15);
    final JTextField user = new JTextField(15);
    final JTextField pass=(JTextField)new JPasswordField(15);
    final JTextField port = new JTextField(15);
    final JButton start = new JButton("Start ");
    //layout design
    gc.anchor = GridBagConstraints.LINE_END;
    gc.weightx = 0.5;
    gc.weighty = 0.5;
    gc.gridx=0;
    gc.gridy=0;
    add(labelhost,gc);
    gc.gridx=0;
    gc.gridy=1;
    add(labeluser,gc);
    gc.gridx=0;
    gc.gridy=2;
    add(labelpass,gc);
    gc.gridx=0;
    gc.gridy=3;
    add(labelport,gc);
    gc.anchor = GridBagConstraints.LINE_START;
    gc.gridx=1;
    gc.gridy=0;
    add(host,gc);
    gc.gridx=1;
    gc.gridy=1;
    add(user,gc);
    gc.gridx=1;
    gc.gridy=2;
    add(pass,gc);
    gc.gridx=1;
    gc.gridy=3;
    add(port,gc);
    gc.anchor = GridBagConstraints.FIRST_LINE_START;
    gc.weighty=10;
    gc.gridx=1;
    gc.gridy=4;
    add(start,gc);      
    gc.gridx=1;
    gc.gridy=5;
    add(pbar,gc);
    gc.gridx=1;
    gc.gridy=6;
    add(lb,gc);





start.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {

             ///mainFrame.paninst.setVisible(false);
            timer.restart();
            pbar.setVisible(true);
            pbar.setValue(0);



              String str = "<html>" + "<font color=\"#008000\">" + "<b>" + 
              "downloading in progress......." + "</b>" + "</font>" + "</html>";
                lb.setText(str);
                //connection
            String shost = host.getText();
            String suser = user.getText();
            String spass = pass.getText();
            String sportb = port.getText();
            int sport = Integer.parseInt(sportb);

            hConnection s = new hConnection(shost, suser, spass, sport);
            Thread thread = new Thread(s);
            thread.setDaemon(true);
            thread.start();

        }


    });
port.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){

        //mainFrame.paninst.setVisible(false);
        timer.restart();
        pbar.setValue(0);

        pbar.setVisible(true);



          String str = "<html>" + "<font color=\"#008000\">" + "<b>" + 
          "Downloading in progress......." + "</b>" + "</font>" + "</html>";
            lb.setText(str);
            //connection
        String shost = host.getText();
        String suser = user.getText();
        String spass = pass.getText();
        String sportb = port.getText();
        int sport = Integer.parseInt(sportb);

    hConnection s = new hConnection(shost, suser, spass, sport);
        Thread thread = new Thread(s);
        thread.setDaemon(true);
        thread.start();


    }});
//Create a timer.
  timer = new Timer(interval, new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  if (i == 100){
  Toolkit.getDefaultToolkit().beep();
  timer.stop();
  pbar.setValue(0);
  String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + 
"download completed." + "</b>" + "</font>" + "</html>";
  lb.setText(str);
  }
  i = i + 1; //i++
  pbar.setValue(i);



  }
  });

}


}

as you can see i am calling new class hconnection using new thread because in my main class i have used the following code

   public class download {
    public static void main(String[] arg){

SwingUtilities.invokeLater( new Runnable(){
    public void run(){
    JFrame frame = new mainFrame("download");
    frame.setVisible(true);
    frame.setSize(700,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.setResizable(false);

    }
});

}
}

so now the problem is when i click on jprogressbar it will run for the first time but when i click it again it is showing me the progress 100 and not starting from 0 and even not reaching the download complete code which it was for the first time..i can see changing of lable within the same class but after creation of new thread it get stopped..please help

Upvotes: 0

Views: 82

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

You never reset the i variable between runs. This means on the second run, i is already 100 (greater)

Upvotes: 1

Related Questions