1

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 ?

LionO
  • 13
  • 2

1 Answers1

0

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());
        }

    }
}
meverett
  • 921
  • 5
  • 6