-2

Hello guys i want to make ProgressBar who will loading till the song starts ... i have this code but the ProgressBar is starting when the song starts and also its maked to finish it about 5 sec. so please someone help me make it right ... Thanks :)

package passwordsaver1;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;

public final class Music extends javax.swing.JFrame {

void centreWindow() {
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((dimension.getWidth() - getWidth()) / 2);
    int y = (int) ((dimension.getHeight() - getHeight()) / 2);
    setLocation(x, y);
}

public Music(String mainAccName) {
    initComponents();
    centreWindow();
    setTitle("PasswordSaver - Music");
    this.mainAccName = mainAccName;
}
String mainAccName;

private void pitbullActionPerformed(java.awt.event.ActionEvent evt) {                                        
    try {
        URL url = new URL("http://mini-mk-market.com/music/PitBulll.wav");
        Clip clip = AudioSystem.getClip();
        AudioInputStream ais;
        ais = AudioSystem.getAudioInputStream(url);
        clip.open(ais);
        clip.loop(5);
        clip.start();
        new Thread(new Start()).start();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Can't find Internet Connection !");
    }
}

// this is just for testing does the song will stop after clicking the new... but cant..
private void afrojackActionPerformed(java.awt.event.ActionEvent evt) {
    try {
        URL url = new URL("http://mini-mk-market.com/music/PitBulll.wav");
        Clip clip = AudioSystem.getClip();
        AudioInputStream ais;
        ais = AudioSystem.getAudioInputStream(url);
        clip.open(ais);
        clip.loop(0);
        clip.start();
        new Thread(new Start()).start();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Can't find Internet Connection !");
    }
}

public static void main(String args[]) {
    try {
                for (javax.swing.UIManager.LookAndFeelInfo info:
                javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            //new Music().setVisible(true);
        }
    });
}

private class Start implements Runnable {

    @Override
    public void run() {
        for (int x = 0; x < 101; x++) {
            ProgressBar.setValue(x);
            ProgressBar.repaint();
            try {
                Thread.sleep(50);
            } catch (Exception ex) {
            }
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2133393
  • 11
  • 1
  • 4
  • 5
    This is a lot fo code. Can you boil it down to a minimal example that demonstrates the problem? For example, none of the look-and-feel or window centering stuff seems directly relevant. – Mike Samuel Mar 04 '13 at 21:12
  • 3
    Don't update/create ANY UI component from outside the Event Dispatching Thread! – MadProgrammer Mar 04 '13 at 21:20
  • show me some exsample i cant get it like this ... :S – user2133393 Mar 04 '13 at 21:26
  • 1
    [crossposted](http://www.daniweb.com/software-development/java/threads/449191/progress-bar) – mKorbel Mar 04 '13 at 21:37
  • Im just searching some fast answer i cant wait ... :/ – user2133393 Mar 04 '13 at 21:47
  • 1
    @user2133393: It's OK to cross-post, but if you do, please have the decency to let us know up front so we can check out all cross-posts before answering. It can upset us as you can imagine, if we answer a question and find that it's been already answered elsewhere. Most of us greatly appreciate it if you show some respect for our right to our free time. – Hovercraft Full Of Eels Mar 04 '13 at 22:18
  • 1
    _Im just searching some fast answer i cant wait_ in that case, your best option is to hire a consultant - s/he'll be at your service and do all _your_ work immediately (provided the payment is good enough ;-) – kleopatra Mar 05 '13 at 11:36
  • ok all right i got it ... i make the progres bar to run but now i need to make the song to stop after playing the new one how to make that anyone help ? – user2133393 Mar 05 '13 at 21:09

1 Answers1

3

You may be looking for ProgressMonitorInputStream. There 's a discussion in How to Use Progress Monitors, and an example in ProgressMonitorDemo.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045