Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help--- JButton background color Posted by mollie on 24 Feb 2011 at 7:44 AM
I have a JButton that when it is pressed, the background color of it changes. My problem now is how can I change it to its original color when it is pressed again? Here is my code so far..

JButton f= new JButton("G");
	f.setBounds(62, 84, 45, 30);
	f.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0) {
			f.setBackground(Color.CYAN);
		}});

Report
Re: Help--- JButton background color Posted by silveredge52 on 25 Feb 2011 at 9:53 AM
Hey,
Use couple of variables to keep track of prior background color, ie.
            public void actionPerformed(ActionEvent arg0) {
                if (firstTime) {
                    priorColor = Color.CYAN;
                    firstTime = false;
                }
                holdPriorColor = f.getBackground();
                f.setBackground(priorColor);
                priorColor = holdPriorColor;
            }

regards, se52



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.