Jump to content

Disable Button on Timer?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
oldezwe

oldezwe

    Newbie

  • Members
  • Pip
  • 8 posts
I'm not familiar with timers.

I have a button. I want it to be disable for a set time after being clicked, and then enabled again.

How do I implement it being disabled on a timer in it's action?

Image cImage3 = getToolkit().createImage("buttonpressed.png");

		ImageIcon cIcon3 = new ImageIcon(cImage3);

		button1.setPressedIcon(cIcon3);

		Image cImage = getToolkit().createImage("buttonover.png");

		ImageIcon cIcon = new ImageIcon(cImage);

		button1.setRolloverIcon(cIcon);

		Image cImage2 = getToolkit().createImage("button.png");

		ImageIcon cIcon2 = new ImageIcon(cImage2);

		button1.setIcon(cIcon2);

		button1.setBorder(null);

		button1.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

		button1.setHorizontalAlignment(JButton.LEADING); // optional

		button1.setBorderPainted(false);

		button1.setContentAreaFilled(false);

		button1.addActionListener(new ActionListener(){

		public void actionPerformed(ActionEvent e) {

			

}


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Easiest: (not necessarily the most beautiful code :p )
public void actionPerformed(ActionEvent e){

    (new Thread(new Runnable(){

          public void run(){

               button1.setEnabled(false);

               Thread.sleep(5000);

               button1.setEnabled(true);

          }

     })).start();	

}
5000 = 5 seconds.
Thread.sleep requires a try catch, but i don't know which catch by heart. Your IDE should tell you that :)

#3
oldezwe

oldezwe

    Newbie

  • Members
  • Pip
  • 8 posts
This answer doesn't disable my button.

if i click while the button is supposed to be disabled, it still goes through with the action.

still need help

thanks

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

This answer doesn't disable my button
Yes it does...

#5
oldezwe

oldezwe

    Newbie

  • Members
  • Pip
  • 8 posts
lol. nah bruh

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Yes it does :D >> YouTube - Yes it does




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users