Hi everyone,

I'm having trouble inserting icons into a textpane and I'm not sure why. I'd like to set up a textpane and then be able to insert and display images in that textpane during runtime. As a test I've written the code below for a simple GUI with a textpane and a button - when the button is pressed I want the image to appear in the text pane under the text that's already there.

If anyone can point out where I'm going wrong here, or if there is a better way to go about this, I'd be very grateful.


package inserticon;

import javax.swing.ImageIcon;

public class Display extends javax.swing.JFrame

    // Variables 

    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;

    // Constructor

    public Display()

    // Method to intialise the GUI

    private void initComponents() 
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jButton1 = new javax.swing.JButton();


        jTextPane1.setFont(new java.awt.Font("Tahoma", 0, 16)); 
        jTextPane1.setText("Some fairly boring text.");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 16)); 
        jButton1.setText("Insert icon");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,  layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))


    // Method it insert an icon into the textpane when the button is pressed.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
        jTextPane1.insertIcon( new ImageIcon( "icon.png" ) );

what behavior are you getting? is the icon getting inserted at all?

the java page on jtextpane says about insert

"Inserts an icon into the document as a replacement for the currently selected content. If there is no selection the icon is effectively inserted at the current position of the caret. This is represented in the associated document as an attribute of one character of content."

so it inserts in the selected area or position of caret.


I added a main method so that the program runs.

public static void main(String args[]){
   	Display dis=new Display();

I put an icon.png in the same folder.
both the program and the button works. The problem is that the icon will be located before the text rather than after the text.
So the question has become: How to make icon inserted after the text? I think, adams161 has quoted the answer:"....the icon is effectively inserted at the current position of the caret. ". So "before inserting, put the cursor after the text" is the solution.

The output is shown in the image: icon.gif

Thanks for the help guys!

Not at all. Please make sure you mark the thread as solved if your queries have been answered.