Artifacts when changing background colour of JTextArea
Asked Answered
D

4

7

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

Desinence answered 10/10, 2011 at 23:48 Comment(2)
Please have a look at Rob Camick's blog on this: backgrounds-with-transparencyRhythmandblues
@EdStaub: My "answer" is only a a link and thus should be a only comment.Rhythmandblues
O
2

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();
    }
};
Omura answered 21/11, 2012 at 1:36 Comment(0)
D
1

It's because you are using a partially transparent color for the component's background. Try setting your background color's alpha channel value to 255 and see if the artifacts still show up. The call to repaint() fixes the issue because it forces the underlying buffer to be filled with your background color prior to painting the text (I think).

Dahomey answered 18/1, 2012 at 19:29 Comment(0)
Z
-1

I had the same issue with a project I worked on for school recently. You have to call repaint on the frame too (so I changed the ActionListener to take a JFrame in the constructor). I also rearranged the code to use the content pane of the JFrame. 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();
    } 
}
Zoomorphism answered 18/10, 2011 at 5:23 Comment(0)
D
-1

I had similar problems and resolved them by using the validate() method on the component in question. So many things it could be... maybe I'll get slammed for this but - speaking as one who has just spent an entire year laboring with Swing - I say to you: RUN!! Swing is just about deprecated.

Learn JavaFx 2.0 and help bury Swing.

Donatist answered 22/4, 2012 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.