Question

I'm having trouble with my animations and my JComboBox. In my application, I can move and transform my shapes either by using the mouse and keyboard or by setting animations.

My component hierarchy is as follows : a JPanel placed in my JFrame contains a panel called EditorCanvas where shapes are modified and drawn, and a panel called DrawMenu containing some JButtons and my JComboBox.

JFrame -> JPanel -> EditorCanvas
JFrame -> JPanel -> DrawMenu -> JComboBox

The JComboBox is used to choose a shape to be added to the canvas on a click. In another thread I call repaint() on my canvas every 10ms after having computed the new positions, etc...

The problem is that I cannot use my JComboBox anymore, as the popup disappear immediately after I open it. And this is caused at some point by my repaints. The thing that is really weird is that my JComboBox is placed in another panel, so it should not be repainted.

I tried to replace my repaint calls in a Runnable called by SwingUtilities.invokeLater, but the problem remained the same.

Here are some relevant parts of my code :

public class EditorCanvas extends JPanel implements MouseListener, MouseMotionListener, KeyListener {

    ...

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        // To keep keyboard focus
        this.requestFocusInWindow();

        this.graphics = (Graphics2D) g.create();

        this.graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        this.mainRenderer.paintAll(this.graphics);
    }


public class AnimationManager implements Runnable {

    ...

    public void run () {

        try {

            while (Thread.currentThread() == this.thread) {

                // Update time values
                double curTime = System.currentTimeMillis();
                double deltaTime = curTime - this.lastTime;
                this.lastTime = curTime;

                for (Animation anim : this.animations) {
                    anim.update(deltaTime);
                }

                SwingUtilities.invokeLater(new Runnable() {
                    public @Override void run() {
                        EditorParameters.getCanvas().repaint();
                    }
                });

                // Pause the animation if it has to
                synchronized (this) {

                    while (this.pause) {
                        System.out.println("\nAnimation paused!\n");
                        this.wait();
                    }
                }

                Thread.sleep(SLEEP_TIME);
            }
Was it helpful?

Solution

The problem is that I cannot use my JComboBox anymore, as the popup disappear immediately

Painting method are for painting only.

this.requestFocusInWindow();

You should never invoke a method like that in a painting method.

I don't even know why you need a custom panel. You should NOT be invoking the paintAll() method of another component. Swing will look after painting all the child components of a panel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top