简体   繁体   中英

JTextField flashing white when typing

I am actively rendering using a BufferStrategy in my own render loop. Here is the method that takes care of the repainting.

public void handleRepaint() {
    try {
        Graphics g = buffer.getDrawGraphics();
        render(g);
        g.dispose();
        if (!buffer.contentsLost()) {
            buffer.show();
        }
    } catch (IllegalStateException e) {
    }
}

The details of the render(Graphics g) method are a bit unnecessary to display. Basically I am doing my custom repainting, and then calling contentPane.paintComponents(Graphics g) , with the contentPane being the content pane of my JFrame , cast into a JComponent .

This works perfectly when the components are opaque. However, when any component is transparent, weird things start to happen. For example, if I create a JButton and add it to the JPanel , the background of the button occasionally flashes white when the mouse enters/exists it. When I add a JTextField , typing will occasionally cause the entire text field to flash white.

I can't figure out what is going on. It obviously has something to do with the active rendering, but I can't figure out what it is. Even with the tutorial I used , the same thing happens when I set any of their JButtons to be transparent.

I'm at a loss as to how to fix this problem. With the buttons, disabling the rollover works, but I don't want the rollover to be disabled. And with JTextFields and other components, there is no rollover to be disabled, and I can't just disable typing.

So if anyone has a solution to this, please help.

PS: The repaint code for the swing components are all run on the EDT.

However, when any component is transparent, weird things start to happen.

Whenever I see a comment like that I suggest you check out Backgrounds With Transparency for potential problems and a couple of solutions.

Basically, because the background is transparent you need to make the component non-opaque so its parents background is painted first, then you need to do custom painting to paint your components background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM