Pregunta

Easy Java question here. I have made a custom JSlider which paints squares at the last location the user has moved the slider to. We override paintcomponent and draw the squares relative to the size of the slider. Everything works good when you use the slider properly. Problems happen though when you resize the frame. The slider grows bigger, and the slider adjuster moves with it, but our squares that we placed don't move relative to the slider. Which function should we use from JSlider to repaint when we resize the frame?

I am trying to keep the JSlider code separate from the frame code, so we want the user to be able to use our custom JSlider and not be trying to handle this resizing feature themselves. Any help on which method we should use for this?

Thanks.

¿Fue útil?

Solución

You can add a ComponentListener to your slider that repaints it on componentResized.

slider.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        e.getComponent().repaint();
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top