0

Following this guide I want to add a JProgressBar in my application while a long PDF document is being created after the apposite button is pressed. This is a skeleton of my method:

/* asking data to database and getting a list of Objects */
JFrame frameBar = new JFrame("Avanzamento");

Container content = frameBar.getContentPane();
JProgressBar progressBar = new JProgressBar();
progressBar.setValue(0);
progressBar.setStringPainted(true);
Border border = BorderFactory.createTitledBorder("Creazione report PDF in corso");
progressBar.setBorder(border);
content.add(progressBar);
frameBar.setSize(600, 200);
frameBar.setVisible(true);

int length = results.getProtocolloList().size();

for (int j = 0; j < length; j++) {

/* print object j-th in a table into the PDF Document */
progressBar.setValue((length / 100)*j);

Thread.sleep(1000);

}

frameBar.setVisible(false);
frameBar.dispose();

Anyway nothing is showed in the frameBar, what I see is just a blank window without anything. Do you know where am I making the mistake?

SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • 1
    What you're doing wrong is that you're running a long process on the Swing event thread, tying it up, preventing it from drawing the GUI. The solution is to use a SwingWorker or other background thread to do your long-running code **off** of the Swing event thread. Please read [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) for the details. – Hovercraft Full Of Eels Dec 31 '14 at 16:33
  • Ehm...this is my first experience with Swing, I am afraid I do not understand the problem well. Should I use SwingUtilities.invokeLater method in which update the bar? – SagittariusA Dec 31 '14 at 16:38
  • No, that won't solve anything. Please read the tutorial as it will explain all. – Hovercraft Full Of Eels Dec 31 '14 at 16:40
  • Sorry but now is worse than before...I need one or two hints – SagittariusA Dec 31 '14 at 16:57
  • 1
    Hint: 1) do the long-running task within the `doInBackground()` method of a SwingWorker. 2) Inside of this method, as your background process progresses, set the SwingWorker's progress property via its `setProgress(...)` method. 3) Add a PropertyChangeListener to your SwingWorker to you can monitor the value of its progress property, and then use this value to update your JProgressBar. For example, please see my code in [this answer](http://stackoverflow.com/a/10240173/522444) as well as my code in [this similar question and answer](http://stackoverflow.com/a/25694838/522444). – Hovercraft Full Of Eels Dec 31 '14 at 17:32
  • thanks...I will have a trial as soon as possible tomorrow and let you know :) – SagittariusA Dec 31 '14 at 17:49
  • @HovercraftFullOfEels hi, first of all happy new year 2015 and thank you very much for your help. I've just finished to write a sample code based on yours (you can have a look here, http://pastebin.com/VDwD106Z) and it seems to work. I have understood many new things but there is a part which is a total black hole to me and I do not understand how it works. I am referring to the lines signed with /**/ in my code. If you can explain me something, I would be very grateful to you :)) – SagittariusA Jan 01 '15 at 12:38
  • To understand PropertyChangeListeners, you will want to first get a basic understanding of the [Observer Design Pattern](http://en.wikipedia.org/wiki/Observer_pattern). This is a programming construct where one object registers a listener on another, allowing it to respond to changes in the second object's state. Space and time constraints prevent me from saying much more, but I'd be happy to explain any specifics if you're still stuck, but you'll need to ask a more specific question. – Hovercraft Full Of Eels Jan 01 '15 at 14:55
  • You've already done too much. Thank you for your help. There are still many things I don't know concernig object programming...hope I'll get them through experience. Still thanks! :) – SagittariusA Jan 01 '15 at 15:16

0 Answers0