Artifacts when changing the background color of JTextArea

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)); // repaint(); } }; public static void main(String[] args) { new Test(); } } 

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.

example image

+6
source share
4 answers

You just do not have enough text, I think

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

This is because you are using a partially transparent background color of the component. Try setting the background alpha channel value to 255 and see if artifacts continue to appear. Calling repaint() fixes the problem because it causes the base buffer to fill with the background color before drawing the text (I think).

0
source

I had the same problem with a project that I recently worked at school. You should also call redraw in the frame (so I changed the ActionListener to take the JFrame in the constructor). I also rebuilt the code to use the JFrame content panel. This seems to work for me:

  public Test() { this.area = new JTextArea(); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(area, BorderLayout.CENTER); JButton button = new JButton(new MyClickAction(this)); button.setText("Click Me!"); this.getContentPane().add(button, BorderLayout.SOUTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setPreferredSize(new Dimension(500, 200)); this.area.setText("this is just a test"); this.pack(); this.setVisible(true); } public static void main(String[] args) { new Test(); } private class MyClickAction extends AbstractAction { private JFrame frame; public MyClickAction(JFrame frame) { this.frame = frame; } @Override public void actionPerformed(ActionEvent e) { area.setBackground(new Color(0, 0, 123, 138)); frame.repaint(); } } 
-1
source

I had similar problems and resolved them using the validate () method for the component in question. So many things can be ... maybe I will slam shut for this, but, speaking, like the one who just spent a whole year working with Swing, I tell you: RUN !! Swing is almost out of date.

Learn JavaFx 2.0 and help bury Swing.

-1
source

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


All Articles