Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Artifacts when changing background colour of JTextArea

I'm having a problem when setting the background colour of a JTextArea after I set its text. The code is as follows:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Test extends JFrame {

    private JTextArea area;

    public Test() {
        this.setLayout(new BorderLayout());
        this.add(this.area = new JTextArea(), BorderLayout.CENTER);
        this.add(new JButton(clickAction), BorderLayout.SOUTH);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(500, 200));
        this.pack();
        this.area.setText("this is just a test");
        this.setVisible(true);
    }

    Action clickAction = new AbstractAction("Click") {
        @Override
        public void actionPerformed(ActionEvent e) {
            area.setBackground(new Color(0, 0, 123, 138));
            // repaint();
        }
    };

    public static void main(String[] args) {
        new Test();
    }
}

If I click the button, the background of the JTextArea changes, but I also get some artifacts in the text area. The "repaint" seems to fix it, but in my application example, it doesn't help, so I was wondering whether there is a better solution to this.

example image

like image 200
Max Avatar asked Oct 10 '11 23:10

Max


1 Answers

You're just missing one text i think

Action clickAction = new AbstractAction("Click") {
    @Override
    public void actionPerformed(ActionEvent e) {
        area.setBackground(new Color(0, 0, 123, 138));
        area.repaint();
    }
};
like image 174
deimOoniGht Avatar answered Sep 18 '22 21:09

deimOoniGht