I had a problem setting the background color of 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));
If I click the button, the background of the JTextArea will change, but I will also get some artifacts in the text area. Repainting seems to fix this, but in my sample application this does not help, so I was wondering if there is a better solution for this.
source share