Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing and Thread  RSS feed

 
francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Respected Sirs,

I have a GUI application with events. when some action generate I take some data from server(my swing application is a WebService client part). This may take some times. I want to display a JProgressBar with a meassage "Searching Please Wait". For this I am use Thread. As soon as the action generates I call the start() of Thread. But this is not work.

if(ae.getSource()==source)
{
mythread.start();

// code for server datas


}

public void run()
{

while(mybar.getValue()!=100)
{
try{
mylabel.setText(mybar.getValue()+1+"is loaded");
mybar.setValue(bar.getValue()+1);
Thread.sleep(1000);

}
catch(Exception e)
{e.printStackTrace();}
}
}

These two are not work simultaneously.

Now , the progresbar just starting(ie one second). and then // code for server datas // executing. the remaing part of bar is not filling(thread not working)


What is the probelm ?

Thanks in advance
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only the event dispatching thread is allowed to change the values in a swing component. What you are doing is correct, in that you should use a separate thread to do long running actions.

However, this separate thread is not allowed to access swing components. To do that, you should write *yet* another runnable class that will be dispatched to the event dispatching thread to execute the small parts that change the component. To dispatch a runnable to the event dispatching thread, use the invokeLater() or invokeAndWait() methods of the SwingUtilities class.

Henry
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to do is instantiate and display progress-bar in event method (where you're checking the source of event), and start a thread which performs the time-taking communication with server.

Never, I repeat, Never perform any time-taking task in event methods.

Also, see if FoxTrot interests you. It has its shortcomings, but can be very useful if used appropriately.
 
francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not working, please check it,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Demo extends JFrame implements ActionListener
{
JProgressBar bar = new JProgressBar();
JButton start=new JButton("Start");
JLabel l=new JLabel();
Demo()
{
Container con=getContentPane();
con.setLayout(new FlowLayout());
con.add(bar);
con.add(l);
con.add(start);
start.addActionListener(this);
setSize(400,300);
setVisible(true);
}



public static void main(String[] args)
{
new Demo();
}

void doWork()
{
System.out.println("end"+bar.getValue());

while(bar.getValue()!=100)
{
try{
l.setText(bar.getValue()+1+"is loaded");
bar.setValue(bar.getValue()+1);
Thread.sleep(1000);

}
catch(Exception e)
{e.printStackTrace();}
}
}



public void actionPerformed(ActionEvent ae)
{


Runnable doWorkRunnable = new Runnable() {
public void run() { doWork(); }
};

SwingUtilities.invokeLater(doWorkRunnable);


}


}
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go back and reread some of the remarks in this topic... but I'll summarize the two most important points.

1. You can't perform any time consuming tasks in any event handlers. This will have the effect of hanging the GUI, as it will not be able to perform anything else.

2. You can't access any Swing components outside of the event dispatching thread, as it is not safe to do so -- doing so will have undetermined results.

You have to obey both restrictions -- your orig program obeyed rule #1, but broke #2. Your new version obeyed rule #2, but broke #1.

Henry
[ December 18, 2006: Message edited by: Henry Wong ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!