java button pressed for 3 seconds

2020-04-16 06:40发布

问题:

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?

回答1:

May this help you

btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            btn.setEnabled(false);
            doSomething() ;
            btn.setEnabled(true);
        }
    });


回答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.



回答3:

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



回答4:

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;
            }
        });
    }
});


回答5:

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);
} 


标签: java button swt