Results 1 to 5 of 5
Thread: Progressbar
- 07-16-2009, 02:04 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
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] } }
-
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
- 07-16-2009, 03:01 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
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); }
-
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.
- 07-16-2009, 03:32 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
Similar Threads
-
ProgressBar with Timer
By Zepp in forum NetBeansReplies: 1Last Post: 04-06-2009, 04:40 AM -
problems with progressbar
By komunista88 in forum Threads and SynchronizationReplies: 1Last Post: 01-05-2009, 09:02 PM -
ProgressBar Demonstration
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks