Skip to main content

A swing jButton issue

3 replies [Last post]
emesol01
Offline
Joined: 2008-01-12

Dear all;

seems I've got a little problem with my jubutton here. I've a jubtton that when u click on it it launches a method which do some long-time URL connection. My problem is that teh button keeps sticking till the called method is done then it gets released. I experience the same thing with my combobox too. Is there a way to release these buttons manually?

here is the code:

jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) CreateDocument(); //this is the methdo that takes a long time
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
irond13
Offline
Joined: 2007-06-26

Hi,

Swing 101: no long running tasks should be performed on the Event Dispatch Thread (EDT), the thread where all GUI tasks are performed. Swing runs only on the EDT, so as soon as you run anything on the EDT, you prevent Swing from doing its thing so long as that task is still running. This isn't usually a problem for short tasks, but for longer tasks it is - as you've already experienced. Consequently, even if you did manually "depress" your button, nothing would happen until your long-running task was complete. The solution to your problem can be found here: http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html .

Regards,
Pierre

BTW, the Swing forum can be round here: http://forums.java.net/jive/forum.jspa?forumID=74

emesol01
Offline
Joined: 2008-01-12

Thanks alot;

I saw the reply but actually I'm new to Java programming and my knowledge of threads is still basic...do you have an idea if I the same problem will arise if I use the AWT buttons and comboxes instead of SWING?

thx

irond13
Offline
Joined: 2007-06-26