Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6845501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:30:29+00:00 2026-05-27T00:30:29+00:00

I have two class fils upload.java and transferProgress.java . upload.java makes applet GUI and

  • 0

I have two class fils upload.java and transferProgress.java. upload.java makes applet GUI and uploads file to remote SSH server. transferProgress.java class gives transfer percentage. The upload percentage completed can be seen in console, but i wanted it to be seen on TextArea and on java progress bar. So i made transferProgress.java to inherit upload.java and append into TextArea.

My problem is, TextArea and JProgressBar do not get updated during the file transfer, but gets updated only when the file transfer is completed. After transfer completion, TextArea shows the log and JProgressBar get set to 100%. My code does not updates TextArea and JProgressBar during file transfer.

If i use setText() instead of append for updating TextArea, i can see real time progress update, but still ProgressBar do not get updated on real time.

I could not figure out where the problem is. I will be very thankful to your help.

upload.java

package biforce;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*; 
import java.io.*; 


import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;


public class upload extends Applet
{

  String filename;
  int port = 22;
  String user = "root";
  String password = "mypass";
  String host = "192.168.0.5";
  String knownHostsFile = "/home/bishwo/.ssh/known_hosts";
  String sourcePath = "";
  String destPath = "/media/dfs/gwas/";

  JTextField txtField = new JTextField(20);
  static TextArea txtArea;
  static JProgressBar progressBar;

    @Override
  public void init(){

  // text Field     
  txtField.setEditable(false);
  txtField.setText("");

  // text area
  txtArea = new TextArea(4,40);
  txtArea.setEditable(false);

  // JprogressBar
  progressBar = new JProgressBar(0, 100);
  progressBar.setValue(0);
  progressBar.setStringPainted(true);

  // Label
  JLabel fileLabel = new JLabel("File");

  // buttons
  JButton upload = new JButton( "Upload" );
  JButton browse = new JButton( "Browse" );

  // browse file to be uploaded
  browse.addActionListener( 
      new ActionListener()
      {
            @Override
        public void actionPerformed( ActionEvent ae )
        {
          JFileChooser fc = new JFileChooser();
          fc.setCurrentDirectory( new File( "/home/bishwo/Desktop/" ) );
          int returnVal = fc.showOpenDialog( upload.this ); 
          String filePath="";
          if ( returnVal == JFileChooser.APPROVE_OPTION )   
          {  
            File aFile = fc.getSelectedFile();  
            filePath = aFile.getAbsolutePath(); 
            filename = aFile.getName();
          }
          txtField.setText(filePath);
        }
      });
  // upload the browsed file
  upload.addActionListener( 
      new ActionListener()
      {
            @Override
        public void actionPerformed( ActionEvent ae )
        {

          if(txtField.getText().length()==0)
          {
              JOptionPane.showMessageDialog(upload.this,"Please select a file to upload.","Error", JOptionPane.ERROR_MESSAGE);
          }
          else
          {
            try
            {
                sourcePath=txtField.getText();
                JSch jsch = new JSch();
                jsch.setKnownHosts(knownHostsFile);
                Session session = jsch.getSession(user, host, port);
                session.setPassword(password);
                session.connect(); 

                ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
                sftpChannel.connect();

                txtArea.setText("Uploading..");
                transferProgress progress = new transferProgress();
                sftpChannel.put( sourcePath,destPath+filename, progress);      
                System.out.println("\nUpload Completed");  

                sftpChannel.exit();
                session.disconnect();
                JOptionPane.showMessageDialog(upload.this,"Upload completed successfully.","Info", JOptionPane.INFORMATION_MESSAGE);

            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(upload.this,e,"Error", JOptionPane.ERROR_MESSAGE);
            }
          }
        }
      });

  add(fileLabel);
  add(txtField,"center");
  add(browse);
  add(upload);
  add(progressBar);
  add(txtArea);
  }

}

transferProgress.java

import com.jcraft.jsch.*;

public class transferProgress extends upload implements SftpProgressMonitor
{
    public double count=0;
    private int percentage;
    public double totalSize;
    private int lastPercentage;
    @Override
    public void init(int op, String src, String dest, long max) 
        {
        this.totalSize=max;
        }

    @Override
    public boolean count(long count) 
        {
        this.count += count;
        this.percentage = (int) ((this.count / this.totalSize) * 100.0);
        if (this.lastPercentage <= this.percentage - 5) 
            {
            this.lastPercentage= this.percentage;
            // setValue() does not work
            biforce.upload.progressBar.setValue(20);
            // append() does not work
            biforce.upload.txtArea.append(Integer.toString(this.percentage));
            // displays percentage completion on console
            System.out.println("Upload Completion "+this.percentage+" %");

            }
        return true;
        }

    @Override
    public void end() 
        {
        System.out.println("Total Copied "+this.percentage+" %");
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:30:30+00:00Added an answer on May 27, 2026 at 12:30 am

    count() method from transferProgress class gives progress percentage 20 times until the file gets uploaded.

    In my case SwingUtilities.invokeLater did not work out. I used SwingWorker, executed from upload class and it worked.
    (P.S – i made progressBar in the class upload ‘public’)

    public class uploadWorker extends SwingWorker<Integer, Integer>
    {
        @Override
            protected Integer doInBackground() throws Exception
            {
                    //some codes .....
    
                    try{sftpChannel.put(upload.sourcePath,upload.destPath, new transferProgress()); }
                   catch(Exception e){System.out.println(e);}
    
                    Thread.sleep(1000);
                    return 42;
            }
    
            protected void done()
            {
    
                    System.out.print("done");
    
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two class: //file FruitTree.h @interface FruitTree : NSObject { Fruit
trying to have two class that reference each others, in the same file. What
I have two classes: class A and class B. Class A makes a number
I'm going to have two class functions. The first class function opens the file.
I have two class (AppDelegate and WebViewController) with two nib file: one is the
I have two class as trial.java and ImageWoLab0.java(Image without lable 0).When I run my
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have two classes class A { public string something { get; set; }
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
i have two models: class Category has many :jobs end class Job belongs_to :category

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.