问题
I am trying to change the color of a JToggleButton
when it has been selected in a reliable, look and feel independent way.
If using the Metal L&F, then using the UIManager is an approach:
UIManager.put("ToggleButton.selected", Color.RED);
Note: 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:
UIManager.put("ToggleButton.select", Color.RED);
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 Color
-based UIManager
properties for ToggleButton
at all, or it at least does not supply them itself (there is a quick example online to find all property keys from the UIManager, which in the example is conveniently limited explicitly to Color
properties).
I have tried setting the background color:
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);
Not only does it not change the selected state, but that does not even effect the unselected state.
I have tried changing the background color only after receiving the action:
@Override
public void actionPerformed(ActionEvent e)
{
JToggleButton button = (JToggleButton)e.getSource();
if (button.isSelected()) // alternatively, (Boolean)getValue(Action.SELECTED_KEY)
{
button.setBackground(Color.RED);
}
}
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):
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);
}
}
}
That is slightly modified from a comment in this Java bug report. Interestingly (amusingly?), in claims to have been fixed in 1998.
Does anyone know of a better, L&F independent way to set the background color of a selected JToggleButton?
回答1:
You might see if setIcon()
is sufficient for your purpose, but you can also override paint()
in the ButtonUI delegate.
Addendum: @kleopatra's comment is well-taken: changing the UI delegate is not trivial. @mKorbel's recent example shows both the difficulty and versatility of the approach. Its essential advantage is look & feel independence.
Some less ambitious approaches are mentioned here.
回答2:
"ToggleButton.selected" is wrong, it require "ToggleButton.select". And should be update to the component.
UIManager.put("ToggleButton.select", Color.WHITE);
SwingUtilities.updateComponentTreeUI(togglebuttonname);
回答3:
JToggleButton btn = new JToggleButton(...);
btn.setUI(new MetalToggleButtonUI() {
@Override
protected Color getSelectColor() {
return Color.RED;
}
});
回答4:
You can simply force background color before each repaint - for that you have to alter paintComponent, check if button is toggled, set background depending on toggle state and, at last, let super class do the actual paint job:
public class ColoredToggleButton extends JToggleButton
{
@Override
public void paintComponent(Graphics g)
{
Color bg;
if (isSelected()){
bg = Color.GREEN;
} else {
bg = Color.RED;
}
setBackground(bg);
super.paintComponent(g);
}
}
回答5:
When it using "Windows look and feel"; in Netbeans you have to do only two things.
- Go to properties
- Un-select 'contentAreaFilled'
- Select 'opaque'
- You can set a background color in properties or by hard coding
In other way,
jToggleButton.setContentAreaFilled(false);
jToggleButton.setOpaque(true);
jToggleButton.setBackground(Color.red); //Your color here
That's all.:-)
回答6:
If you would rather use an action listener instead of overriding methods in a UI you can just change the UI to a UI without any selectColor
properties.
Here is an example I used recently
private class FavouriteToggle extends JToggleButton {
public FavouriteToggle() {
setUI(new BasicToggleButtonUI()); //Removes selectColor
////Your Custom L&F Settings////
setBackground(new Color(255, 252, 92));
setForeground(Color.GRAY);
setText("Favourite");
setBorder(null);
setFocusPainted(false);
////Add your own select color by setting background////
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(((JToggleButton) e.getSource()).isSelected()) {
setForeground(Color.BLACK);
setBackground(new Color(255, 251, 0));
} else {
setBackground(new Color(255, 252, 92));
setForeground(Color.GRAY);
}
}
});
}
}
来源:https://stackoverflow.com/questions/5808022/changing-the-background-color-of-a-selected-jtogglebutton