5 Replies - 619 Views - Last Post: 25 April 2011 - 03:20 AM Rate Topic: -----

#1 frank_drebin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-March 11

Updating Swing components when a button is clicked again

Posted 24 April 2011 - 10:16 PM

Hi everyone.

So I have a button that when clicked, prints something to a JTextField. What do I need to use if I want the JTextField area to be cleared if the button is clicked again?

Thanks a lot for your responses in advance! :)
Is This A Good Question/Topic? 0
  • +

Replies To: Updating Swing components when a button is clicked again

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Updating Swing components when a button is clicked again

Posted 24 April 2011 - 10:22 PM

You could keep track of a boolean or a counter variable of some sort. In your actionPerformed() method, check if it is of a certain value. If so, clear your JTextField and reset the counter or boolean. Otherwise, set the variable to an appropriate value.
Was This Post Helpful? 0
  • +
  • -

#3 frank_drebin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-March 11

Re: Updating Swing components when a button is clicked again

Posted 24 April 2011 - 10:56 PM

View Postmacosxnerd101, on 24 April 2011 - 10:22 PM, said:

You could keep track of a boolean or a counter variable of some sort. In your actionPerformed() method, check if it is of a certain value. If so, clear your JTextField and reset the counter or boolean. Otherwise, set the variable to an appropriate value.


Hi.

Thank you so much for that! :)

I just have one more question, I've set the button that I was talking about to-
-
myButton.setBackground(Color.red);
 myButton.setOpaque(true);



When I tried to execute code that should execute-
-
 myButton.setOpaque(false)



It remains highlighted as red. How can I get the button to go back to how it was before ie. before I set its background color to red and its opacity to true?

Thanks a lot once again! :)
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Updating Swing components when a button is clicked again

Posted 25 April 2011 - 02:42 AM

Color myOldColor = myButton.getBackground();
...
myButton.setBackground(Color.red);
myButton.setOpaque(true);
...
myButton.setBackground(myOldColor);
myButton.setOpaque(false);




Just save the old color and then set that as the "old" color.
Was This Post Helpful? 1
  • +
  • -

#5 frank_drebin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-March 11

Re: Updating Swing components when a button is clicked again

Posted 25 April 2011 - 03:07 AM

View PostDogstopper, on 25 April 2011 - 02:42 AM, said:

Color myOldColor = myButton.getBackground();
...
myButton.setBackground(Color.red);
myButton.setOpaque(true);
...
myButton.setBackground(myOldColor);
myButton.setOpaque(false);




Just save the old color and then set that as the "old" color.


Thank you so much!!

That was incredibly useful! :D
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Updating Swing components when a button is clicked again

Posted 25 April 2011 - 03:20 AM

Glad I could help. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1