开发者

SwingX and JToggleButton

In my application i'm using JXButton from the SwingX library, i really like the painter methods. Now i have to change my simple开发者_如何转开发 button to toggle button and have different painter for the unselected/selected state. Unfortunately i couldn't find JXToggleButton. Is there a solution to keep the benefit of painter methods ?


It's actually fairly easy to create a JXType component. You may need to do some extra checks for borders, focus, etc.. but this is the general approach you would take

public class JXToggleButton extends JToggleButton implements FocusListener, Paintable {
    private Painter<JTextField> backgroundPainter;
    private Painter<JTextField> foregroundPainter;

    public void setBackgroundPainter(Painter<JTextField> painter) {
        this.backgroundPainter = painter;
    }

    public void setForegroundPainter(Painter<JTextField> painter) {
        this.foregroundPainter = painter;
    }

    @Override
    protected void paintComponent(Graphics g) {

        if (backgroundPainter != null) {
            this.backgroundPainter.paint((Graphics2D) g, this, getWidth(), getHeight());
        }

        super.paintComponent(g);

        if (foregroundPainter != null) {
            foregroundtPainter.paint((Graphics2D) g, this, getWidth(), getHeight());
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜