Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-05-2009, 12:33 PM
Member
 
Join Date: Jan 2009
Posts: 1
Rep Power: 0
komunista88 is on a distinguished road
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) {
;
}
}
}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-05-2009, 09:02 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 798
Rep Power: 1
Steve11235 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ProgressBar Demonstration Java Tip SWT 0 07-11-2008 04:52 PM
many to many problems cecily Database 1 08-02-2007 05:51 PM
gui problems bluebirdjc Advanced Java 2 07-23-2007 05:38 PM
a few problems gary AWT / Swing 0 07-11-2007 04:57 PM
Problems with the jar Freddie NetBeans 2 05-29-2007 03:50 PM


All times are GMT +2. The time now is 03:12 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org