Jump to content

Help in GUI ProgressBar JAVA

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
mr_skyflakes21

mr_skyflakes21

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
I'm trying to simulate the jobs in CPU scheduling algorithm and i come up with the idea of using the JProgressBar,

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)

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


#2
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Well at first glance your error points to the fact that your trying to get the first element out of your arraylist thats empty in your iterate method. Hence the null pointer exception - theres nothing in the array!

I'm going mess with your code a bit now.

#3
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
You need to resize your window, but as it is, when the window loads stretch it out.

Click the add button, then click the start button. I think you want it to show in increments of 1 right? Well then you need to do a refresh after every loop in that case.
(Hint, you need to use threads).

Also, you can only show 1 bar at the moment, but this should get you started.

Hope that helps.

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, start;
    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();
        
        find = new JButton("Add");
        find.setActionCommand("Add");
        find.addActionListener(this);
        pane.add(find);
        
        start = new JButton("Start");
        start.addActionListener(this);
        pane.add(start);        
        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);			
			
		}
		if(start == event.getSource()){
			iterate();
		}
	}
    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();
   }
}

Edited by Stu_328, 05 May 2009 - 11:24 AM.


#4
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Here I made this last night, but didnt get to package this till now.

I presume you want something like this? Just double-click it.

Attached Files



#5
mr_skyflakes21

mr_skyflakes21

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Wow the JProgressBar works perfectly when i ran it.

Would you mind if I ask for the code?

So that i can review it and make some variations.

Thanks!