Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 15, 2006, 11:41 PM   #1
CANEHDN
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, UT
Button changing colors in Java

I am a student and I'm having a hard time figuring out how to make it so when you press a button the color of the button will change. It would cycle through 5 different colors. Any ideas would be much appreciated.
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!
CANEHDN is offline   0 Reply With Quote
Old Feb 16, 2006, 12:50 AM   #2
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by CANEHDN
I am a student and I'm having a hard time figuring out how to make it so when you press a button the color of the button will change. It would cycle through 5 different colors. Any ideas would be much appreciated.
The default User Interface look and feel on OS X does not allow you to change the background color of buttons since OS X buttons in native applications generally have a white background. The default UI look and feel is designed to match the OS X interface look as much as possible.

The sample code below does precisely what you are asking about. Change the flag "USE_CROSS_PLATFORM_UI" to "true" and compile and run the code to see the different look and feels.

(Ignore the coding style ... I just threw it together to illustrate the original question.)

Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonDemo extends JPanel implements ActionListener {

	private static boolean USE_CROSS_PLATFORM_UI = false;
	
	int buttonLabelIndex = 0;
	String buttonLabels[] = { "Blue", "Cyan", "Green", "Magenta", "Orange", "Pink", "Red", "Yellow" };
	Color  buttonColors[] = { Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.YELLOW};
	JButton button;
	
	public ButtonDemo() {
		super(new BorderLayout());
		
		button = new JButton(buttonLabels[buttonLabelIndex]);
		// In the default UI look and feel you cannot easily alter the background color
		// for buttons since it is designed to match the OS X UI.
		if(USE_CROSS_PLATFORM_UI) {
			button.setBackground(buttonColors[buttonLabelIndex]);
		} else {
			button.setForeground(buttonColors[buttonLabelIndex]);
		}
		
		button.addActionListener(this);
		this.add(button, BorderLayout.CENTER);
		
		this.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
	}
	
	public void actionPerformed(ActionEvent e) {
		buttonLabelIndex = ++buttonLabelIndex < buttonLabels.length?buttonLabelIndex:0;

		button.setText(buttonLabels[buttonLabelIndex]);
		
		if(USE_CROSS_PLATFORM_UI) {
			button.setBackground(buttonColors[buttonLabelIndex]);
		} else {
			button.setForeground(buttonColors[buttonLabelIndex]);
		}
	}

	
	private static void run() {
		if(USE_CROSS_PLATFORM_UI) {
			try {
				UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		JFrame frame = new JFrame("Button Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JComponent contentPane = new ButtonDemo();
		contentPane.setOpaque(true);
		
		frame.setContentPane(contentPane);
		frame.pack();
		
		frame.setVisible(true);
	}
	
	public static void main(String[] args) {
		run();
	}

}
mrichmon is offline   0 Reply With Quote
Old Feb 16, 2006, 10:30 AM   #3
CANEHDN
Thread Starter
macrumors 6502a
 
CANEHDN's Avatar
 
Join Date: Dec 2005
Location: Sandy, UT
Thanks for the help. This is what I needed.
__________________
:Macbook Pro 2.8GHz, 4GB RAM, 9600M GT 512MB VRAM
:Gaming- i7-2600K, GTX 570, Ripjaw 8GB RAM, SABERTOOTH P67, Thermaltake Element G
I support the MacRumors Blood Drive!
CANEHDN is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Mac newbie - How to change colors in iCal? stringtheorist Mac Applications and Mac App Store 22 Sep 12, 2013 06:01 PM
easy question about how to change color in a stylesheet. Please help Rob587 Web Design and Development 2 Jun 29, 2009 05:14 AM
"Coffee Mug Ring Stain" How to change color in PHotoghop? DyslexicArtist Design and Graphics 4 Oct 24, 2008 10:46 AM
Display of colors in Java bluegoo26 Mac Programming 2 Feb 27, 2008 03:35 PM
Changing Colors in Excel Michael7k Mac Applications and Mac App Store 3 Feb 4, 2006 01:21 PM


All times are GMT -5. The time now is 05:41 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC