hello guys i want to make progress bar in netbeans i trying with this code but the progres bar doesnt work property just showing 100% when finish and doesnt have animation of doing that progress:

public final class Music extends javax.swing.JFrame {
public static class Start implements Runnable {

        @Override
        public void run() {
        }
    }
}
    private void afrojackActionPerformed(java.awt.event.ActionEvent evt) {
    new Thread(new Start()).start();
        for (int x = 0; x < 100; x++) {
            ProgressBar.setValue(x);
            ProgressBar.repaint();
            try {
                Thread.sleep(50);
            } catch (Exception ex) {
            }
        }
    }

anyone tell me what is the problem and please done give me the link of http://docs.oracle.com i have seened that many times but cant figure out ... :/

Problem Solved but its stucking till the song starts ...

Recommended Answers

All 7 Replies

You should never call Thread.sleep in the event dispatch thread. I don't know if afrojackActionPerformed is being called in the event dispatch thread, but from its name I guess it is, and that is surely the problem. Events should be handled quickly so that your GUI can handle other events and draw itself.

If you aren't in the event thread, then you probably shouldn't be calling setValue. I guess that ProgressBar is a JProgressBar, but even if it's not, it's clear that setValue is intended to cause some sort of update in the GUI, and doing that is a job for the event dispatch thread.

Here is all code of the Frame:

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) {
                }
            }
        }
    }

So whats is the problem... ? the progress just start when song starts i want to end the progres when song starts .. thanks :)

someone help ??? :S

have you tried keeping the other thread (the song) to be blocked while your progressbar is running?

nope and please tell me how can i make that :)

I wanna make the progressbar loading till the song starts and i dont know how to make that so please show me come code ... :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.