Tags

, , , ,

Dikasih tugas ama bu dosen pemrograman lanjut disuruh bikin contoh program pake thread… Bingung sih tadinya mau bikin apa, pernah ngoprek thread juga pas bikin Splash Screen kemaren… Ya udah deh sekarang bikin simulasi copy file pake JProgressBar…

Kenapa bikin aplikasi kaya gitu?? Ya soalnya JProgressBar kan yang sering gue liat di tutorial bergeraknya pake Thread, ya udah dipake aja deh… Ni screenshootnya :

Gimana cara bikin program kaya di atas?? Simak source code nya :

/*
 *  Copyright (c) 2011 Ganjar Ramadhan.
 *  All rights reserved.
 *
 *  Program ini menggunakan fasilitas thread
 *  untuk membuat animasi JProgressBar saat
 *  proses simulasi menyalin file(ceritanya)... Hehe..
 *
 *  Nah.. Karena JProgressBarnya punya nilai 100
 *  Saya pake iterasi buat ngisi nilai dari JProgressBarnya
 *  Thread.sleep dipake biar ada delay dari iterasi 1 sampai 100
 *  Dengan begitu si JProgressBar akan kelihatan bergerak maju.
 *  Kenapa bikin 2 JProgressBar, sebenernya iseng aja sih...
 *  Kan ceritanya program ini nampilin proses menyalin 5 file mp3.
 *  JProgressBar pertama nampilin proses copy per file...
 *  JProgressBar yang kedua nampilin keseluruhan proses copy...
 *
 *  Info lebih lanjut kunjungi blog saya:
 *  https://ganjarramadhan.wordpress.com
 *
 *  Atau email saya di :
 *  ganjar.ramadhan05@yahoo.com
 */

/*
 * FormCopy.java
 *
 * Created on 23 Mar 11, 13:12:03
 */

package pr1.multithreading;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;

/**
 *
 * @author Ganjar Ramadhan
 */
public class FormCopy extends javax.swing.JFrame {

 class Thread1 implements Runnable{
 String [] file = {"Secondhand Serenade - Fall For You.mp3",
 "Avenged Sevenfold - Dear God.mp3",
 "My Chemical Romance - Welcome To The Black Parade.mp3",
 "Paramore - Decode.mp3",
 "Muse - Neutron Star Collision (Love Is Forever).mp3"
 };
 public void run() {
 for (int j=1; j<=5; j++){
 for (int i=0; i<=100; i++){
 lblLagu.setText(file[j-1]);
 lblAll.setText("Copying File "+j+" of 5");
 pb.setValue(i);
 try {
 Thread.sleep(30);
 } catch (InterruptedException ex) {
 }
 }
 }
 }

 }

 class Thread2 implements Runnable{

 public void run() {
 for (int i=0; i<=100; i++){
 pb1.setValue(i);
 try {
 Thread.sleep(155);
 } catch (InterruptedException ex) {
 }
 }
 JOptionPane.showMessageDialog(FormCopy.this, "5 Files Copied!");
 }

 }

 Thread th1 = new Thread(new Thread1());
 Thread th2 = new Thread(new Thread2());
 private Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
 /** Creates new form FormCopy */
 public FormCopy() {
 initComponents();
 pb.setStringPainted(true);
 pb1.setStringPainted(true);
 this.setLocation((dim.width-this.getWidth())/2, (dim.height-this.getHeight())/2);
 initJajal();
 }

 /** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
 @SuppressWarnings("unchecked")
 // <editor-fold defaultstate="collapsed" desc="Generated Code">
 private void initComponents() {
 //disini otomatis digenerate sama netbeans!!
 }// </editor-fold>

 /**
 * @param args the command line arguments
 */
 public static void main(String args[]) {
 java.awt.EventQueue.invokeLater(new Runnable() {
 public void run() {
 new FormCopy().setVisible(true);
 }
 });
 }

 // Variables declaration - do not modify
 private javax.swing.JLabel jLabel1;
 private javax.swing.JLabel jLabel2;
 private javax.swing.JLabel jLabel3;
 private javax.swing.JLabel lblAll;
 private javax.swing.JLabel lblLagu;
 private javax.swing.JProgressBar pb;
 private javax.swing.JProgressBar pb1;
 // End of variables declaration

 private void initJajal(){
 th1.start();
 th2.start();
 }
}

Kalo ada yang mau download projectnya silahkan download di sini.

Advertisements