Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging the background color of a selected JToggleButton
    primarykey
    data
    text
    <p>I am trying to change the color of a <code>JToggleButton</code> when it has been selected in a reliable, look and feel independent way.</p> <p>If using the Metal L&amp;F, then using the UIManager is an approach:</p> <pre><code>UIManager.put("ToggleButton.selected", Color.RED); </code></pre> <p><strong>Note</strong>: Iyy pointed out that I had a typo in the property name above, but I will leave it above for people getting here, but the actual property name is supposed to be:</p> <pre><code>UIManager.put("ToggleButton.select", Color.RED); </code></pre> <p>However, this does not work in my current Look and Feel (currently Windows XP). After some further analysis, it appears that the system look and feel in Windows (still XP) does not use any of the <code>Color</code>-based <code>UIManager</code> properties for <code>ToggleButton</code> at all, or it at least does not supply them itself (there is a quick example online to <a href="http://alvinalexander.com/java/java-uimanager-color-keys-list" rel="noreferrer">find all property keys from the <code>UIManager</code></a>, which in the example is conveniently limited explicitly to <code>Color</code> properties).</p> <p>I have tried setting the background color:</p> <pre><code>Action action = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { /* stuff */ } }; JToggleButton button = new JToggleButton(action); // tried with and without opaque true button.setOpaque(true); button.setBackground(Color.RED); </code></pre> <p>Not only does it not change the selected state, but that does not even effect the unselected state.</p> <p>I have tried changing the background color only after receiving the action:</p> <pre><code>@Override public void actionPerformed(ActionEvent e) { JToggleButton button = (JToggleButton)e.getSource(); if (button.isSelected()) // alternatively, (Boolean)getValue(Action.SELECTED_KEY) { button.setBackground(Color.RED); } } </code></pre> <p>None of that works. The only thing that I have found to work requires me to draw the button myself in the selected state (which leads to a working example, albeit non-standard looking):</p> <pre><code>private class ColoredToggleButton extends JToggleButton { ColoredToggleButton(Action action, Color color) { super(action); setBackground(color); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (this.isSelected()) { int w = getWidth(); int h = getHeight(); String s = getText(); // selected color g.setColor(getBackground()); g.fillRect(0, 0, w, h); // selected foreground color g.setColor(SystemColor.controlText); g.drawString(s, (w - g.getFontMetrics().stringWidth(s)) / 2 + 1, (h + g.getFontMetrics().getAscent()) / 2 - 1); } } } </code></pre> <p>That is slightly modified from a comment in this <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4105766" rel="noreferrer">Java bug report</a>. Interestingly (amusingly?), in claims to have been fixed in 1998.</p> <p>Does anyone know of a better, L&amp;F independent way to set the background color of a selected JToggleButton?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload