• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Disabling a button for a few seconds

 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have an application where we want to prevent the user from hitting a submit button 20 times in a row, in case the refresh is a little slow. This seems easy, but I'm not quite getting it right. I know I'm missing something obvious. Here's my code:


I don't think I should even need to m_allowTradeSubmission.
The problem is the events still see to be queued oup in the event queue and just take longer to execute.
Any ideas? Surely there's a code snippet out there somewhere.
--Mark
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after sleeping of thread as much u need just called
JButton a = new JButton();
sleep(1000);
a.setVisible(false);
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try disabling the button first thing in the action performed. Changing the cursor to show that processing is taking place. Re-enabling the button and cursor when the processing is complete.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the problem lies in the event Threads of Swing. When you click a button that performs some action, the button actually stays pressed until the action is complete. No matter how long it takes. So you really shouldn't be having a problem of the user pressing the button 20 times, because you shouldn't be able to as long as the actionPerformed method is still being executed.
If you wanted, you could use the SwingUtitlies.invokeLater() method. That is how you deal with event Threads in Swing. That way, you can press a button and use the invokeLater method so that the button is then released and the "work" or actions are carried out in a seperate thread from the Click event Thread. Then you could also disable the button, and have the Event you are processing notify you when the button can be active again.
Hope that helps a little. I may have rambled.
 
Mark Herschberg
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can trying pushing the button multiple times (although I sugest you decrease the timeout), I push it once, and then very quickly (in less then 5 secons), push it again. You'll see that in both cases, it increments and prints the count.

Originally posted by Paul Stevens:
You could try disabling the button first thing in the action performed. Changing the cursor to show that processing is taking place. Re-enabling the button and cursor when the processing is complete.


I tried that in the code above--I removed the if, and disabled the button, so it only got reenabled at the end of the thread. That didn't work either.
--Mark
[ February 28, 2003: Message edited by: Mark Herschberg ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That does sound odd. And you are right. I however don't know enough about it to determine the cause. I bet Nathen does if he would get his sorry butt in here.
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol - you'll kick your own butt here...
as you know, swing will queue events when the dispatch thread is hung... You ATTEMPT TO create a seperate thread to disable the button and wait to re-enable. However, this is not acting multithreaded as you are calling the run method of this thread object instead of the start method. calling the run method does just that - it calls the run method in the same thread!!!
Now here's a much simpler solution (excuse the nasty exception handling, and the name change) example only:
package com.dana.nothing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
public class Nothing extends JFrame {
JButton myButton = new JButton();
public Nothing() throws Throwable {
try {
jbInit();
}catch(Exception e) {
e.printStackTrace();
}
this.pack();
this.setVisible(true);
}
private void jbInit() throws Exception {
myButton.setText("jButton1");
myButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
myButton_actionPerformed(e);
}
});
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(myButton, null);
}
void myButton_actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
myButton.setEnabled(false);
new Thread(new Runnable() {
public void run() {
try {
//i dont understand why you don't just do your
//business logic here instead of sleeping,
//but this is what you requested...
Thread.sleep(4000);
} catch (InterruptedException ex) {
}
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
myButton.setEnabled(true);
}
});
} catch (InvocationTargetException ex) {
} catch (InterruptedException ex) {
}
}
}).start();
//here is where you could launch a thread
//to do business logic, but it makes more sense up above
}
public static void main(String[] args) throws Throwable {
new Nothing();
}
}
 
Mark Herschberg
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dana Hanna:
lol - you'll kick your own butt here...
as you know, swing will queue events when the dispatch thread is hung... You ATTEMPT TO create a seperate thread to disable the button and wait to re-enable. However, this is not acting multithreaded as you are calling the run method of this thread object instead of the start method. calling the run method does just that - it calls the run method in the same thread!!!



Ouch! Yeah, I gave myself a swift one. :-)
Run... start... but I'm rusty with threads. EJBs have made me soft. :-)
Thanks!!!
--Mark

PS I'm not sure if there's any benefit to putting the business logic in the thread. I put it directly in the action performed method. What is the motivation for doing otherwise?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, seems like I suggested SwingUtilities too. I guess next time I should provide an example.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally got my butt here... but it looks like Dana beat me to the punch!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic