-1

I have a problem. I need to create a GUI with a JTextArea, and inside it I want to set a background image. I have created a class which extends JTextArea and overridden the paintComponent method to insert the image.

The image is now visible but when I call the setText method, the text is hidden behind the image. How can resolve it?

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Samael
  • 3
  • 4
  • 1
    Show the relevant code please – Sybren May 13 '15 at 16:45
  • @Sybren How will the code help, since if we do what the OP describes the outcome will simply be what they describe? What will you learn from examining the code? (Code which, based on their description is clearly functioning correctly, although it does not do what they want it to.) – Radiodef May 13 '15 at 16:55

2 Answers2

1

We can do this through setting a custom UI:

static void decorate(JTextArea a, final BufferedImage img) {
    a.setUI(new javax.swing.plaf.basic.BasicTextAreaUI() {
        @Override
        protected void paintBackground(Graphics g) {
            g.drawImage(img, 0, 0, null);
        }
    });

    a.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
    a.setForeground(Color.white);
    a.setCaretColor(Color.lightGray);
}

See BasicTextUI#paintBackground.

img area

(Image from here.)

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125
0

Try calling setOpaque(false) in the constructor of your class that extends JTextArea. Having opaque set to true causes the JTextArea's color background and text to show.

package demo;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class BackgroundDemo extends JFrame{

    public BackgroundDemo() {
        initUI();
    }

    public void initUI() {
        MyTextArea area = new MyTextArea();
        add(area);
        area.setText("Demo Text");

        pack();
        setSize(400, 400);
    }

    public static void main(String[] args) {
        BackgroundDemo demo = new BackgroundDemo();
        demo.setVisible(true);
    }
}

class MyTextArea extends JTextArea {

    public MyTextArea() {
        setOpaque(false);
        setVisible(true);
        setPreferredSize(new Dimension(400, 400));
    }

   @Override
   public void paintComponent(Graphics g) {
       super.paintComponent(g);

       try {
           BufferedImage image =    ImageIO.read(BackgroundDemo.class.getResource("/demo/background.png"));
           g.drawImage(image, 0, 0, this);
       } catch(IOException ioe) {
           ioe.printStackTrace();
       }
   }

}
  • So are you saying that perhaps they could paint the image on a panel *behind* the text area? (Or perhaps paint the image on the text area, and call `super.paintComponent` afterwards?) – Radiodef May 13 '15 at 17:09
  • I added some code, and yes to using super.paintComponent. – ArcaneEnforcer May 13 '15 at 17:29