Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Created June 5, 2017 11:38
Show Gist options
  • Save Blasanka/8586628c714b340c316a5f9eeeb36ff1 to your computer and use it in GitHub Desktop.
Save Blasanka/8586628c714b340c316a5f9eeeb36ff1 to your computer and use it in GitHub Desktop.
JProgressBar Example(Only how progress bar works not functional).
This is a sample example for `JProgressBar`. This
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class MD5 extends JFrame{
private JProgressBar progressBar = new JProgressBar(0, 100);
public MD5(){
this.setTitle("Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(progressBar);
this.setSize(400, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void run() {
progressBar.setIndeterminate(true);
Worker task = new Worker();
task.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if ("progress".equals(e.getPropertyName())) {
progressBar.setIndeterminate(false);
progressBar.setValue((Integer) e.getNewValue());
}
}
});
task.execute();
}
class Worker extends SwingWorker<Double, Double> {
private static final int N = 5;
double x = 1;
@Override
protected Double doInBackground() throws Exception {
for (int i = 1; i <= N; i++) {
x = x - (((x * x - 2) / (2 * x)));
setProgress(i * (100 / N));
publish(Double.valueOf(x));
Thread.sleep(1000);
}
return Double.valueOf(x);
}
@Override
protected void process(List<Double> list) {
for (double d : list) {
try {
generateHash();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//-----------------------Your hash generate function-------------------------------------
public void generateHash()throws Exception{
MessageDigest md = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream("C:\\file.txt");
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
};
byte[] mdbytes = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println(sb.toString());
}
//----------------------------------------------------------------------------
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MD5 md = new MD5();
md.run();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment