Skip to main content

Inserting picture into jtextarea

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
mozum
Offline
Joined: 2012-05-01

Is it possible to insert picture into jtextarea. If it is, please kindly assist me with how to do it.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
skynet357
Offline
Joined: 2012-10-10

Im not sure if this is what you r looking for. But after few googling found a way to insert background image.

public final class AreaTest extends JFrame
{
    public AreaTest(final Image image)
    {
        Container container = getContentPane();
        container.setLayout(new BorderLayout());

        JTextArea textArea = new JTextArea()
        {
            //Image grayImage = GrayFilter.createDisabledImage(image);
            {setOpaque(false);}

            public void paintComponent (Graphics g)
            {
                g.drawImage(image, 0, 0, (int)getSize().getWidth(), (int)getSize().getHeight(), this);
                //g.drawImage(grayImage, 0, 0, (int)getSize().getWidth(), (int)getSize().getHeight(), this);
                super.paintComponent(g);
            }
        };

        container.add(textArea);
    }


    public static void main(String[] args)
    {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image image = toolkit.createImage("RedDevil.png");

        AreaTest areaTest = new AreaTest(image);
        areaTest.setSize(300, 300);
        //areaTest.setResizable(false);
        areaTest.setTitle("Area test");
        areaTest.setVisible(true);
    }
}