Results 1 to 2 of 2
  1. #1
    komunista88 is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default problems with progressbar

    hi, i've tried to make a smiple player using to buttons RESUME and SUS
    PEND and a progressbar that will show me track duration so when the song is finished it's completed. but it doesnt works.it always shos the full progress bar?
    this is te first class:
    *************************************
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.*;

    public class ImageAudioAnimation extends JApplet {
    static final int MY_MINIMUM = 0;
    static final int MY_MAXIMUM = 100;
    private final static int broj_na_nacii = 7;
    private int br = 0;
    private ImageIcon[] ikoni = new ImageIcon[broj_na_nacii];
    private AudioClip[] audioClips = new AudioClip[broj_na_nacii];
    private AudioClip brAudioClip;

    private int[] delays = {48000, 54000, 59000, 54000, 59000, 31000, 68000};
    private Timer timer = new Timer(delays[0], new TimerListener());

    private JLabel lblImage = new JLabel();
    private JButton btnResume = new JButton("Resume");
    private JButton btnSuspend = new JButton("Suspend");
    private JComboBox cmbNacii = new JComboBox(new Object[]{"Denmark", "Germany", "China", "India", "Norway", "UK", "US"});

    public void init() {
    for (int i = 0; i < broj_na_nacii; i++) {
    ikoni[i] = new ImageIcon(getClass().getResource("image/flag" + i + ".gif"));
    audioClips[i] = Applet.newAudioClip(getClass().getResource("audio/anthem" + i + ".mid"));
    }
    cmbNacii.setRenderer(new CountryCellRenderer());
    JPanel panel = new JPanel();
    final SwingProgressBarExample it = new SwingProgressBarExample();
    panel.add(it);
    panel.add(btnResume);
    panel.add(btnSuspend);
    panel.add(new JLabel("Izberi"));
    panel.add(cmbNacii);
    this.getContentPane().add(lblImage, BorderLayout.CENTER);
    this.getContentPane().add(panel, BorderLayout.SOUTH);
    for (int j= 0;j<6;j++)
    for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
    final int percent = i;
    try {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    it.updateBar(percent);
    }
    });
    java.lang.Thread.sleep(delays[j]/100);
    } catch (InterruptedException e) {
    ;
    }
    }
    btnResume.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    start();
    }
    });
    btnSuspend.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    stop();
    }
    });
    cmbNacii.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    stop();
    br = cmbNacii.getSelectedIndex();
    tekovnaNacija(br);
    timer.start();
    }
    });

    timer.start();
    lblImage.setIcon(ikoni[0]);
    lblImage.setHorizontalAlignment(JLabel.CENTER);
    brAudioClip = audioClips[0];
    brAudioClip.play();
    }

    private class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    br = (br + 1) % broj_na_nacii;
    tekovnaNacija(br);
    }
    }

    private void tekovnaNacija(int index) {
    lblImage.setIcon(ikoni[index]);
    cmbNacii.setSelectedIndex(index);
    brAudioClip = audioClips[index];
    brAudioClip.play();
    timer.setDelay(delays[index]);
    }

    public void start() {
    timer.start();
    brAudioClip.play();
    }

    public void stop() {
    timer.stop();
    brAudioClip.stop();
    }

    }

    class CountryCellRenderer extends JLabel implements ListCellRenderer {
    private static final Color HIGHLIGHT_COLOR = new Color(0, 0, 128);
    public CountryCellRenderer() {
    setOpaque(true);
    setIconTextGap(12);
    }

    public Component getListCellRendererComponent(JList list, Object value,
    int index, boolean isSelected, boolean cellHasFocus) {
    String entry = (String) value;
    setText(entry);
    setIcon(new ImageIcon(getClass().getResource("image/" + entry + ".gif")));
    if (isSelected) {
    setBackground(HIGHLIGHT_COLOR);
    setForeground(Color.white);
    } else {
    setBackground(Color.white);
    setForeground(Color.black);
    }
    return this;
    }
    }
    ************************************************** **
    and this is the progress bar class:
    ******************************
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.SwingUtilities;

    public class SwingProgressBarExample extends JPanel {

    JProgressBar pbar;

    static final int MY_MINIMUM = 0;

    static final int MY_MAXIMUM = 100;

    public SwingProgressBarExample() {
    pbar = new JProgressBar();
    pbar.setMinimum(MY_MINIMUM);
    pbar.setMaximum(MY_MAXIMUM);
    add(pbar);
    }

    public void updateBar(int newValue) {
    pbar.setValue(newValue);
    }

    public static void main(String args[]) {
    int z=5000,j=4000;
    final SwingProgressBarExample it = new SwingProgressBarExample();

    JFrame frame = new JFrame("Progress Bar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setContentPane(it);
    frame.pack();
    frame.setVisible(true);

    for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
    final int percent = i;
    try {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    it.updateBar(percent);
    }
    });
    java.lang.Thread.sleep(10);
    } catch (InterruptedException e) {
    ;
    }
    }
    }
    }

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    First, you are updating the progress bar very rapidly, so that it should complete in around a second.

    Second, always do setVisible() using the following code:
    [CODE]
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new ProfileView().setVisible(true);
    }
    });
    [CODE]

    This causes the method to run on the event dispatcher thread, where it belongs.

    Third, you didn't wait between setVisible() and updating the progress bar. Between that and the rapid updates, no wonder it finished before you could see it.

    Slow down your loop, and add a Thread.sleep(5000) before starting the loop.

Similar Threads

  1. ProgressBar Demonstration
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:52 PM
  2. many to many problems
    By cecily in forum JDBC
    Replies: 1
    Last Post: 08-02-2007, 05:51 PM
  3. gui problems
    By bluebirdjc in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2007, 05:38 PM
  4. a few problems
    By gary in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2007, 04:57 PM
  5. Problems with the jar
    By Freddie in forum NetBeans
    Replies: 2
    Last Post: 05-29-2007, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •