Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Setting JToggleButton's select color a real pain - any suggestions?

843805Sep 2 2005 — edited Jan 27 2010
Hi,

I'm starting to get a bit frustrated trying to set the select color of a JToggleButton. I feel like I've wasted hours on something that should be a one liner, so I figured it's time to ask for help (again).

I'm trying to create a very simple panel of about five toggle buttons, in a ButtonGroup, with a grid layout (1 column, 5 rows). Basically I want to create a little navigation panel that resembles the left side of the preferences dialogs in Firefox / Thunderbird (if you've seen it). However, I'm having trouble setting the select color of the toggle buttons. I know it's possible to set the color using the UIManager / UIDefaults (I don't use orange, it's just an example)...
UIManager.put("ToggleButton.select", Color.ORANGE);
... but I don't want to change the select color for every toggle button in my application.

I've also seen posts that recommend extending the ButtonUI of the look and feel that is being used. I don't think this is a good solution because I don't intend to extend the ButtonUI of every look and feel I want (or may want) to use.

Although I try to be picky about using quality code that is reusable, I'm at the point where I'm pretty much willing to use anything that will work, no matter how ugly. If you want to share in my frustration, try the following:

Create a tiny app with two JToggleButtons. Label one of them "Go" and the other "Stop". Add the Go and Stop buttons to a button group so that only one may be selected at a time. Make it so when the Go button is pushed it toggles on and is green. Make it so when the Stop button is pushed it toggles on and is red.

In fact, here's a quick hack of my tiny application. Just modify it to set the select color of stop to red and the select color of go to green.
import javax.swing.*;
import java.awt.*;

public class StopAndGo
{
	public StopAndGo()
	{
		JFrame mainFrame = new JFrame("Stop And Go");
		JPanel contentPanel = new JPanel(new FlowLayout());
		JToggleButton stopButton = new JToggleButton("Stop");
		JToggleButton goButton = new JToggleButton("Go");
		ButtonGroup group = new ButtonGroup();

		group.add(stopButton);
		group.add(goButton);

		contentPanel.add(stopButton);
		contentPanel.add(goButton);

		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.add(contentPanel);
		mainFrame.pack();
		mainFrame.setVisible(true);
	}

	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				new StopAndGo();
			}
		});
	}
}
As far as I'm concerned setting the select colors shouldn't be more than two 'setSelectColor(Color.SOMETHING)' lines. After all, one of the first things anyone will tell you about a toggle button is that it changes colors when it's pushed on.

BTW, I'm using Java 1.5 on Linux.

Thanks in advance for any help,
Ryan
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 24 2010
Added on Sep 2 2005
5 comments
1,386 views