What i need is, every time i press the button "Add" (which i created)
it will generate a progress bar and automatically start the progress(the 0 - 100%) progress.
Can anyone do it? Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DrawRectPanel1 extends JFrame implements ActionListener{
JPanel pane;
JTextArea out;
JButton find;
Thread runner;
int num = 0;
int counter = 0;
LinkedList<JProgressBar> pBars = new LinkedList<JProgressBar>();
public DrawRectPanel1() {
super("Progress");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = new JPanel();
pane.setLayout(new FlowLayout());
find = new JButton("Add");
find.setActionCommand("Add");
find.addActionListener(this);
pane.add(find);
setContentPane(pane);
}
public void actionPerformed(ActionEvent event){
if(find == event.getSource()){
pBars.add(new JProgressBar(0, 100));
pBars.get(counter).setStringPainted(true);
pane.add(pBars.get(counter));
setContentPane(pane);
}
}
public void iterate() {
while (num <= 100) {
pBars.get(counter).setValue(num);
try {
Thread.sleep(20);
} catch (InterruptedException e) { }
num += 1;
}
num = 0;
}
public static void main(String[] arguments) {
DrawRectPanel1 frame = new DrawRectPanel1();
frame.pack();
frame.setVisible(true);
frame.iterate();
}
}
It throws a runtime error:
Quote
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.LinkedList.entry(LinkedList.java:365)
at java.util.LinkedList.get(LinkedList.java:315)
at DrawRectPanel1.iterate(DrawRectPanel1.java:36)
at DrawRectPanel1.main(DrawRectPanel1.java:50)
at java.util.LinkedList.entry(LinkedList.java:365)
at java.util.LinkedList.get(LinkedList.java:315)
at DrawRectPanel1.iterate(DrawRectPanel1.java:36)
at DrawRectPanel1.main(DrawRectPanel1.java:50)
NOTE: Don't bother about the DrawRectPanel name, the reason is im testing some ways to simulate a job and i come up with a rect but it looks hard to me.
Edited by mr_skyflakes21, 05 May 2009 - 08:45 AM.
Misinsertion of code


Sign In
Create Account


Back to top










