Hi!

My GUI consists of one main window and it takes 5 seconds before the this window is displayed (after pressing on the "Run" button in Eclipse). I want the window to be displayed at once even though it’s fetching tasks from the service. How could I do that and what could cause this 5 minutes' delay?

Thanks!

5 seconds or 5 minutes?
Are you doing those long-running tasks on the Swing event thread? If so, move them to a SwingWorker thread so they execute in the background while the GUI continues to display

your event dispatch thread should be independent of your swing worker thread. the event dispatch loads the GUI while the swingworker does the intensive resource loading. for an example implementation, refer : http://download.oracle.com/javase/tutorial/uiswing/components/icon.html

look for the picture tumbnail displayer example. btw in that example, the event dispatch happens on the class' constructor.

Thank you both for your comments.

As I see, SwingWorker returns some values. Do I understand correctly that SwingUtilities.invokeLater does the same work as SwingWorker, but it does not return anything?

So, now I try to use SwingUtilities.invokeLater in my program (see the code below). I defined that the following two lines of code (proc = new MyProc(); myList = proc.getAllProcesses(); ) cause 5 seconds' delay. Therefore, I put them into SwingUtilities.invokeLater. However, now the program produces an error message (Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException...), because myList is required for creating Swing components. So, what could you suggest me to do in this case?

private void initComponents()
	{--
		SwingUtilities.invokeLater(new Runnable() {
			    public void run() {
		proc = new MyProc();
		myList = proc.getAllProcesses();
			    }
		  });
	
		scrollPane1 = new JScrollPane();
		list1 = new JList();
...

Take a look through this tutorial on concurrency in Swing

SwingWorker invokes a worker thread in the background that does not tie up the event queue. SwingWorker may publish results for updates on the event queue, but it does not have to.

SwingUtilities.invokeLater() schedules a task to run on the event queue. While the task runs, nothing else on the event queue will be processed - so your GUI will be unresponsive pending completion of that task. That is why you don't want to process long-running tasks directly on the event queue.

Ok, thank you Ezzaral! I've updated my code, however I cannot compile it. Eclipse says that there are multiple markers at line 22 - "});" What is actually wrong in this line? I did everything as shown in the example here http://download.oracle.com/javase/tutorial/uiswing/concurrency/simple.html

private void initComponents()
	{
		SwingWorker worker = new SwingWorker<List<Process>, Void>() {
		    @Override
		    public List<Process> doInBackground() {
				proc = new MyProc();
				myList = tasks.getAllProcesses();
				return myList;
		    }
		    
		    @Override
		    public void done() {	
				scrollPane1 = new JScrollPane();
				list1 = new JList();
...
		
				setList();
				setPanel();
				setPopupMenu();
			}
		});	   
	}

Remove the left parenthesis so you just have }; It's just a minor syntax error in the anonymous class declaration.

yes, now I can compile the code. Hmm, I can see just a blank window... So, I use myList = get() to receive myList. But why the window is blank and nothing happens? This is my last code:

private void initComponents()
	{
		SwingWorker worker = new SwingWorker<List<Process>, Void>() {
		    @Override
		    public List<Process> doInBackground() {
				proc = new MyProc();
				myList = proc.getAllTasks();
				return myList;
		    }
		    
		    @Override
		    public void done() {	
		    	try {
		    		myList = get();
				scrollPane1 = new JScrollPane();
				list1 = new JList();
...			
				setList();
				setPanel();
				setPopupMenu();
		        } catch (InterruptedException ignore) {}
		        catch (java.util.concurrent.ExecutionException e) {
		        }
			}
		};			
	}

You have to call worker.execute() to execute the task. Your code above is just the declaration.

The SwingWorker API doc has some notes on its usage as well.

Ok, I've included worker.execute(); just after the line 25 - }; But again - a blank window...:(

Try adding some println statements to monitor what's going on in your task and print stack traces in your catch blocks. You're deliberately blinding yourself to exceptions with empty catch blocks.

I would recommend constructing your UI components as you normally would, outside of the done() method, and just use done() to add the List contents.

Oh, thank you very much! I've removed constructing methods outside of done() and now everything works perfectly!

Great! :)

might be useful, put the

frame.setVisible(true);

as last line of your Frame constructor .

No, actually that isn't relevant to the question at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.