# JToggleButton - Hintergrundfarbe beim Drücken festlegen



## Guest (6. Jun 2008)

Hi,

ist es, ohne sich einen eigenen JToggleButton zu bauen, möglich, die Hintergrundfarbe, die ein gedrückter Button haben soll, festzulegen?

Danke schon mal!


----------



## Gast (6. Jun 2008)

Habs gefunden!

UIManager.put ("ToggleButton.select", Color.BLACK);


----------



## Guest (6. Jun 2008)

Tja gleich das nächste Problem:

Wenn ich das systemeigene Look and Feel verwende funktioniert die Methode nicht mehr.
Jetzt versuche ich, diesen einen Wert im sSystemeigenen Look and Feel zu ändern, aber noch ohne Erfolg.

Wie bringe ich die UIDefaults wieder ins Look and Feel?


```
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					LookAndFeel laf = UIManager.getLookAndFeel();
					UIDefaults defaults = laf.getDefaults();
					defaults.put ("ToggleButton.select", Color.BLACK);
```


----------



## Guest (6. Jun 2008)

Hier ein Demoprogramm, was das Problem verdeutlicht:

*Mit dem Java Look and Feel funktionierts:*

```
public class Togglebuttontest extends JFrame{
	public  Togglebuttontest(){
		UIManager.put ("ToggleButton.select", Color.BLACK);

		JToggleButton button = new JToggleButton("Knopf");		
		this.getContentPane().add(button);
		this.pack();
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new Togglebuttontest();
	}
}
```

*Mit dem nativ Look and Feel funktioniert es nicht:*

```
public class Togglebuttontest extends JFrame{
	public  Togglebuttontest(){
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		UIManager.put ("ToggleButton.select", Color.BLACK);

		JToggleButton button = new JToggleButton("Knopf");		
		this.getContentPane().add(button);
		this.pack();
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new Togglebuttontest();
	}
}
```

Wie lässt sich das lösen?


----------



## Guest (6. Jun 2008)

Gelöst!


```
class ColoredToggleButton extends JToggleButton implements ItemListener{

		protected Color _activatedColor;

		protected Color _deactivatedColor;

		// Standard JToggleButton with ActionListeners attached
		public ColoredToggleButton() {
			super();
			setSelected(false);
		}

		// Standard JToggleButton with ActionListeners attached
		public ColoredToggleButton(String label) {
			super(label);
			setSelected(false);
		}

		public ColoredToggleButton(Color activatedColor, Color deactivatedColor) {
			super();
			_activatedColor = activatedColor;
			_deactivatedColor = deactivatedColor;
			setSelected(false);
			// Set a new UI because of the toggle button
			setUI(new CustomButtonUI(activatedColor));
			setBackground(deactivatedColor);
			setContentAreaFilled(false);
			//addActionListener(this);
			addItemListener(this);
		}

		public ColoredToggleButton(String label, Color activatedColor,
				Color deactivatedColor) {
			super(label);
			_activatedColor = activatedColor;
			_deactivatedColor = deactivatedColor;
			setSelected(false);
			// Set a new UI because of the toggle button
			setUI(new CustomButtonUI(activatedColor));
			setBackground(deactivatedColor);
			setContentAreaFilled(false);
			//addActionListener(this);
			addItemListener(this);
		}

		public Color getActivatedColor() {
			return _activatedColor;
		}

		public Color getDeactivatedColor() {
			return _deactivatedColor;
		}

		// Overloaded in order to paint the background
		protected void paintComponent(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
			int w = getWidth();
			int h = getHeight();
			GradientPaint gradient = new GradientPaint(20, 10, getBackground(), 20, h,getBackground(), true);
			g2.setPaint(gradient);
			g2.fillRoundRect(0, 0, w, h, 10,10);
			super.paintComponent(g);
		}

	
		public void itemStateChanged(ItemEvent arg0) {
			if (!isSelected()){
				setBackground(getDeactivatedColor());
			}
		}

		

	} // of class
	
	class CustomButtonUI extends BasicToggleButtonUI{

		private Color _selectedColor;

		public CustomButtonUI (Color selectedColor){
			_selectedColor = selectedColor;
		}

		public void paintButtonPressed (Graphics g, AbstractButton b){
			if (b.getBackground() != getSelectedColor())
				b.setBackground(getSelectedColor());
		}

		public Color getSelectedColor (){
			return _selectedColor;
		}
	}
```


----------

