0

I want to set background in JTextArea (having transparency), but I don't have any idea. Maybe it's not very important, but I just want have characteristically program. Please help.

This part of code, I create JTextArea.

notatnik_k = new JTextArea();
JScrollPane scrollPane2 = new JScrollPane(notatnik_k);
scrollPane2.setBounds(20,30, 263,200);
notatnik_k.setEditable(false);  
add(scrollPane2);

This photo showing my problem:

Photo inside JTextArea including transparency

Is there a simply way, to do it?

Tomek eM
  • 92
  • 1
  • 1
  • 11

2 Answers2

0

You will need to extend your JTextArea class and create a setBackground(Image image) method. This method would set a field to the image you want to use and then invoke the the repaint method (this.repaint()).

You should then override the paintComponent(Graphics) method to paint the component with the image you set using setBackground(Image image).

schmidt73
  • 181
  • 11
0

I found here on the site this sample of code, and I did a little change but I have one error and don't know why it's incorrect? It's shows error: "The method setBackgroundImage(Image) is undefined for the type JTextArea"

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BackgroundDemo {

    public JTextArea notatnik;

    private static void createAndShowUI() {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (UnsupportedLookAndFeelException ex) {
        }

        final JFrame frame = new JFrame("BackgroundDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel buttonsPanel = new JPanel();

        final JTextArea notatnik = new JTextArea();
        JScrollPane scrollPane1 = new JScrollPane(notatnik);
        scrollPane1.setBounds(20,270, 380,110);


       JButton loadButton = new JButton("Set background");
        buttonsPanel.add(loadButton);

        loadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
                int returnVal = fc.showOpenDialog(frame);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        Image image = ImageIO.read(fc.getSelectedFile());
                        if (image != null)
                            notatnik.setBackgroundImage(image);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        JPanel content = new JPanel(new BorderLayout());
        content.add(buttonsPanel, BorderLayout.SOUTH);

        frame.add(scrollPane1);
        frame.add(content);
        frame.setSize(new Dimension(800, 500));
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);      
    }

    static class MyTextArea extends JTextArea {
        private Image backgroundImage;

        public MyTextArea() {
            super();
            setOpaque(false);
        }

        public void setBackgroundImage(Image image) {
            this.backgroundImage = image;
            this.repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());

            if (backgroundImage != null) {
                g.drawImage(backgroundImage, 0, 0, this);
            }
            super.paintComponent(g);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}
Tomek eM
  • 92
  • 1
  • 1
  • 11
  • The reason you're getting an error is because you've declared `notatnik` as a `JTextArea`, this classes doesn't have a method called `setBackgroundImage`...neither did you create an instance of `MyTextArea`, so it will never work anyway.. – MadProgrammer Oct 19 '14 at 23:13
  • I used the code from the subject of the first response: http://stackoverflow.com/questions/15992831/inserting-an-image-under-a-jtextarea , is it possible to set the transparency of the image in the background? – Tomek eM Oct 20 '14 at 10:18
  • :( I'dont have any idea, how to set image inside JTextArea, not on all the screen. I saw a lot of examples, but all of them are about how to set background on the all screen. – Tomek eM Oct 20 '14 at 14:31
  • You can make the image transparent using a AlphaComposite, but that's another question ;) – MadProgrammer Oct 20 '14 at 19:10
  • AlphaComposite is external library? – Tomek eM Oct 20 '14 at 19:58
  • Nope, it's part of the 2D Graphics API – MadProgrammer Oct 20 '14 at 20:00
  • Ok, maybe You can give me a tips, what should I looking for to find answer for my problem? More keywords? – Tomek eM Oct 20 '14 at 20:16