2

I created a button and I want these:

When the user clicks the button, it stays pressed like 3 seconds. After 3 seconds the button should be again look pressable. So the user cannot click the button again without waiting 3 secs. I tried these:

{
    button3 = new Button(c20, SWT.PUSH | SWT.CENTER);
    button3.setText("QUERY");
    button3.setBounds(205, 131, 62, 40);
    button3.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent event) {
            try {
                start_query();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        setPressedIcon();///??
        public void widgetDefaultSelected(SelectionEvent event) {
        }
    });
}

Do you have any suggestions?

vels4j
  • 11,208
  • 5
  • 38
  • 63
hassasin
  • 141
  • 1
  • 1
  • 7
  • Have you tried using a countdown timer, and disabling the button while it's active? It should be fairly straightforward to disable the button when you press it, and start the timer, then re-enable it when the timer finishes. To make the timer, you can either just use an integer counter, or tap into the system clock using the Timer class. – Hoeloe Dec 10 '12 at 16:59
  • for easiness, can i use 'for loops' for waiting? – hassasin Dec 10 '12 at 17:01
  • @hassasin The swing timer is a better option than using for loops. It was designed for things like this. – WilliamShatner Dec 10 '12 at 17:02
  • 1
    @hassasin for-loops will kill your CPU and make your app unresponsive. Use appropriate Swing timer for this purpose. You should never perform long-running operations on the EDT (Event Dispatching Thread) – Guillaume Polet Dec 10 '12 at 17:05
  • This is the exact same question you asked before. Don't do that. Improve your previous question or ask for clarification of the answers if you are not pleased with them. – Baz Dec 14 '12 at 09:21
  • @hassasin That's no reason to start a new question. Retag the old question and maybe ask for moderator attention to remove the "wrong" answers. – Baz Dec 14 '12 at 09:32
  • ok next time I will do it like that – hassasin Dec 14 '12 at 09:36

5 Answers5

4

May this help you

btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            btn.setEnabled(false);
            doSomething() ;
            btn.setEnabled(true);
        }
    });
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • 5
    That has an implied flaw in it that unless you have the button on it's own thread, the application will hang for 3 seconds, making everything, not just the button, unresponsive. Better to use a Timer and give it a separate condition, outside the event listener. – Hoeloe Dec 10 '12 at 17:10
  • why did i get "The method addActionListener(new ActionListener(){}) is undefined for the type Button" error? – hassasin Dec 11 '12 at 09:03
  • initially you mentioned as swing. swing contains action listener. you can update this in your selection listener – vels4j Dec 11 '12 at 09:09
  • can you tell how i can use Timer, in below answer he/she used timer bu I got the "The constructor Timer(int, new SelectionListener(){}) is undefined" error? what can be the reason? – hassasin Dec 11 '12 at 09:19
  • The reason is that you're not understanding how the basics of Java works. In this case, look up a tutorial on constructors and class definitions. – Hoeloe Dec 11 '12 at 16:35
  • @hassasin were you able to resolve it. if not, let me know I will give u the answer in SWT – vels4j Dec 11 '12 at 17:46
  • ok i couldnt solve it in SWG still dont know how to use actionlistener or timer, thanks – hassasin Dec 12 '12 at 08:16
2

You need to spawn a new thread when the button is clicked.

 button3.addSelectionListener(new SelectionListener() {
    public void widgetSelected(SelectionEvent event) {
        try {
            Thread t = new Thread(){
              int count = 0;
              setPressedIcon();
              while(count < 4){
                 try{
                  Thread.sleep(1000);
                  count++;
                 }
              }
              setUnpressedIcon();
            }
            t.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    setPressedIcon();///??
    public void widgetDefaultSelected(SelectionEvent event) {
    }
});

If you do it in the same thread as your UI, everything will be halted during the 3 seconds.

alistair
  • 1,164
  • 10
  • 27
  • why did i get this error, setPressedIcon();(this method requires a body instead of a semicolon)? – hassasin Dec 10 '12 at 17:16
  • Updating Swing components should not happen from that thread, but from the EDT. And any reason why you use the `while` loop to sleep for some seconds instead of just using `Tread.sleep` with the desired time. – Robin Dec 10 '12 at 17:20
  • It can still happen from the EDT if you use a handler. There was no reason, other than to make it explicit that we're counting seconds. – alistair Dec 10 '12 at 17:31
1
btn.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
   btn.setEnabled(false);
   Timer timer = new Timer( 3000, new ActionListener(){
     @Override
     public void actionPerformed( ActionEvent e ){
       btn.setEnabled( true );
     }
   }
   timer.setRepeats( false );
   timer.start();
 }
});

I took the answer from @vels4j and added the javax.swing.Timer to it

Robin
  • 36,233
  • 5
  • 47
  • 99
  • what do these @Override mean?=) sorry – hassasin Dec 10 '12 at 17:24
  • 1
    It means it overrides a method of the super class, or implements a method from the interface. That is really basic Java knowledge. [First hit in Google](http://stackoverflow.com/q/94361/1076463) – Robin Dec 10 '12 at 17:50
  • also I got this: The constructor Timer(int, new SelectionListener(){}) is undefined – hassasin Dec 11 '12 at 09:05
  • 2
    @hassasin If those kind of error messages are a problem you should really start with a basic Java tutorial – Robin Dec 11 '12 at 10:05
  • I really should. I looked up everywhere I can not find the answer for it, why I am getting this error!!!! – hassasin Dec 12 '12 at 12:19
1

Instead of using a PUSH Button, use a TOGGLE button. This way you can keep it pressed for as long as you require.

final boolean buttonPressed[] = new boolean[] { false };
final Button button = new Button(comp, SWT.TOGGLE);
button.setText("Test");
button.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
        if (buttonPressed[0]) {
            button.setSelection(true);              
            return;
        }

        buttonPressed[0] = true;
        Display.getDefault().timerExec(3000, new Runnable() {
            @Override
            public void run() {
                button.setSelection(false);
                buttonPressed[0] = false;
            }
        });
    }
});
Waqas Ilyas
  • 3,116
  • 1
  • 17
  • 27
0

Try disabling it, putting the thread to sleep for three seconds, then enable it again:

private void setPressedIcon(){
 try {
  button3.setEnabled(false);
  Thread.sleep(3000);
 } catch(InterruptedException e) {
 } finally {
   //reenable the button in all cases
   button3.setEnabled(true);
} 
Eugenio Cuevas
  • 10,858
  • 3
  • 29
  • 51