Results 1 to 5 of 5

Thread: Progressbar

  1. #1
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default Progressbar

    Hi

    I'm making a program that performs a quite long task and I want to use an indetermine progressbar.
    However, the progressbar only starts when my task is completed.
    I've done some searching and I think I have to use threads.
    But when I try it, it doesn't work.
    Could someone make a simple example how I can use this with a progressbar?

    Code.

    Java Code:
    public class LongTask {
    	
    	public static void main(String[] args) {
    		Functions functions = new Functions();
    		functions.run();
    	}
    }
    Java Code:
    public class Functions implements ActionListener, Runnable {
    	[variable declaring]
    	
    	public void run() {
    		window = new JFrame("Task");
    		window.setSize(300,300);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		input = new JTextField(20);
    		submit = new JButton("Do it");
    		progressBar = new JProgressBar(0, 99);
    		
    		submit.addActionListener(this);
    		
    		SpringLayout layout = new SpringLayout();
    		Container contentPane = window.getContentPane();
    		contentPane.setLayout(layout);
    		
    		contentPane.add(input);
    		contentPane.add(submit);
    		contentPane.add(progressBar);
    		contentPane.add(result);
    		
    		layout.putConstraint(SpringLayout.WEST, input, 5, SpringLayout.WEST, contentPane);
    		layout.putConstraint(SpringLayout.NORTH, input, 5, SpringLayout.NORTH, contentPane);
    		layout.putConstraint(SpringLayout.WEST, submit, 5, SpringLayout.WEST, contentPane);
    		layout.putConstraint(SpringLayout.NORTH, submit, 60, SpringLayout.NORTH, contentPane);
    		layout.putConstraint(SpringLayout.WEST, progressBar, 5, SpringLayout.WEST, contentPane);
    		layout.putConstraint(SpringLayout.NORTH, progressBar, 100, SpringLayout.NORTH, contentPane);
    		layout.putConstraint(SpringLayout.WEST, result, 5, SpringLayout.WEST, contentPane);
    		layout.putConstraint(SpringLayout.NORTH, result, 200, SpringLayout.NORTH, contentPane);
    		
    		window.setVisible(true);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		progressBar.setIndeterminate(true);
    		[long task]
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are doing your task on the EDT or "Event Dispatch Thread", the main thread that runs Swing application drawing and user interaction, and this will freeze the GUI. Please read this tutorial: Concurrency In Swing

  3. #3
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    I did read it and I now use SwingWorker but the result is thesame.

    Java Code:
    	SwingWorker<String, Void> task = new SwingWorker<String, Void>() {
    		@Override
    		public String doInBackground() throws Exception {
    			[long task]
    		}
    	}
    Java Code:
    	public void actionPerformed(ActionEvent e) {
    		progressBar.setIndeterminate(true);
    		task.run();
    		progressBar.setIndeterminate(false);
    		progressBar.setStringPainted(true);
    		progressBar.setValue(99);
    	}

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I thought that you would call task.execute() to get a SwingWorker to run, not "run()". Also, have you had a look at the Sun tutorial on ProgressBars? It shows how to have them work with SwingWorker objects. Hope this helps, and kind regards.

  5. #5
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

Similar Threads

  1. ProgressBar with Timer
    By Zepp in forum NetBeans
    Replies: 1
    Last Post: 04-06-2009, 04:40 AM
  2. problems with progressbar
    By komunista88 in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-05-2009, 09:02 PM
  3. ProgressBar Demonstration
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:52 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
  •