質問

I'm trying to skin a JToggleButton with 2 icons for the default and the toggle state. Why would it not change its display anyways, although I set an icon for both states?

package gui;

import java.awt.Image;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JToggleButton;

public class RepeatButton extends JToggleButton {


    private ImageIcon repeatIcon;
    private ImageIcon repeatIconUnchecked;


    public RepeatButton() {
        repeatIcon = new ImageIcon("media_repeat.png");
        repeatIcon.setImage(repeatIcon.getImage().getScaledInstance(repeatIcon.getIconWidth()/2, repeatIcon.getIconHeight()/2,Image.SCALE_AREA_AVERAGING));
        repeatIconUnchecked = new ImageIcon("media_repeat_uncheckedalt.png");
        repeatIconUnchecked.setImage(repeatIconUnchecked.getImage().getScaledInstance(repeatIconUnchecked.getIconWidth()/2, repeatIconUnchecked.getIconHeight()/2,Image.SCALE_AREA_AVERAGING));
        this.setIcon(repeatIcon);
        this.setDisabledIcon(repeatIconUnchecked);
        this.setBorder(null);
    }
}
役に立ちましたか?

解決

The disabled icon is the icon that will be used when your JToggleButton gets disabled by doing:

btn.setEnabled(false);

And has nothing to do with the isSelected state.

You can do it manually by changing the icon by using a listener for the selected state. Or you could use the setSelectedIcon() method for that.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top