Java Swing: ghost text appears when updating text in JTextArea with partially transparent background

So the deal:

I have JPanel and JTextArea inside this. JPanel has a background color that has an alpha value of 200, i.e. You can fend background image through JPanel. Isn't that called partial transparency? In any case, I set JTextArea opaque so that I can fully see this:

JPanel p = new JPanel();
p.setBackground(new Color(237, 234, 215, 200);
JTextArea jta = new JTextArea("Blahblahblahblah");
jta.setOpaque(false);
p.add(jta);

Ok, so when I click the button, the text will change as follows:

jta.setText("new BlahBlah...");

Then this happens: the first text remains there with a new partially transparent film on it. And the text that I added comes out, of course, but, of course, you can see the previous one behind you. When I change the text several times, the ghost disappears.

Screenshot as a link.

( 3 ):
: " № 1 8: ( )? ( : )"
: " № 2 8: ( , )? ( : ! [ , ])"
: "# 8 8: ( )? ( : )"

, , , 8 3, , . , . - ( )?

, , , ! 48 .

PS. , . : , : , , / , , , , . . , .. () , .

an55i

+3
2

.

+2

- , ! :

  • (p ), (jta) Opaque (false);
  • , , - - , , , .. .

? , , .

:

private JTextArea jta;
private JPanel p;

:

jta = new JTextArea("BlahBlahBLAH");
jta.setBackground(new Color(237, 234, 215, 200));
p = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        if(getContentPane().isAncestorOf(jta)) {
            g.setColor(jta.getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }
};

p.setOpaque(false);
jta.setOpaque(false);
p.add(ta);

. , "jta" "p". , - . !

0

Source: https://habr.com/ru/post/1785489/


All Articles