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.
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();
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With