2

_https://i.stack.imgur.com/W2We5.gif

<img src=\"file:b:/smile.gif\" align=\"middle\">"

_https://i.stack.imgur.com/VPHzw.gif

<img src=\"file:b:/smile.gif\">

Required:
_https://i.stack.imgur.com/WlMhG.gif

I need to align the image in the JEditorPane and the image should not affect the height of rows. If I use align=middle - the picture is not aligned and retained influence on the height of rows.

To a solution of similar problems in html I use:

<span style=\"background-image: url('file:b:/smile.gif') 50% 50% no-repeat\"> &nbsp;&nbsp;&nbsp;&nbsp;</span>

But this method does not work in JEditorPane. How can I solve this problem?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1221483
  • 431
  • 2
  • 7
  • 20

1 Answers1

3

To align your Images to the Middle of the JEditorPane, you can use something like this :

editPane.setText("<html><p style = \"text-align:center;\"><img src = " + 
        "\"http://gagandeepbali.uk.to/gaganisonline/images/" + 
        "editsystemvariable2.png\" alt = \"pic\" /></p></html>\n");

Here the text-align property can do that trick for you. And about that thing that image should not affect the size of the row, I am not sure about your intentions on that, but if I understood you right then you can provide a fixed width and height to your images in the <img> tag.

Here I used this code, and tell me if you desire something else, other than what you find in this code. Wish I could help,

import java.awt.*;
import javax.swing.*;

public class EditorPaneTest extends JFrame
{
    public EditorPaneTest()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);        

        JEditorPane editPane = new JEditorPane();
        JScrollPane scrollPane = new JScrollPane(editPane);     

        editPane.setContentType("text/html");

        editPane.setText("<html><p style = \"text-align:center;\">Hello there, How you doing ?<img src = " + 
                                            "\"http://s018.radikal.ru/i504/1202/03/c01a2e35713f.gif" + 
                                                "\" alt = \"pic\" width = \"15\" height = \"15\" />I guess all is good!!" +
                                                        "<br />I hope this is what you wanted!! " + 
                                                                    "<img src =  \"http://s018.radikal.ru/i504/1202/03/c01a2e35713f.gif" + 
                                                "\" alt = \"pic\" width = \"15\" height = \"15\" /> Hope this works for you :-)</p></html>\n");

        add(scrollPane, BorderLayout.CENTER);
        setSize(400, 300);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new EditorPaneTest();
            }
        });
    }
}

And here is the output image :

EditorPane's View

MoreOver use this Smiley Image. I had removed the extra bottom space from the image for you.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Reducing the image size does not solve the problem, it is necessary so that the picture was behind the text that is above and below. Exemple: _http://i.stack.imgur.com/WlMhG.gif – user1221483 Feb 20 '12 at 18:28
  • @user1221483 : May you post the code that you actually using to put images and text together on the JEditorPane. Will appreciate that. – nIcE cOw Feb 20 '12 at 18:30
  • String str = new String(" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaa"aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    "); JEditorPane.setText(str);
    – user1221483 Feb 20 '12 at 18:45
  • Unfortunately the picture is still not in the middle row and still affects the height _http://s017.radikal.ru/i428/1202/08/bc5c139e0c48.png Smile: 20x20px _http://s018.radikal.ru/i504/1202/03/c01a2e35713f.gif There are more flexible alternative to JEditorPane? – user1221483 Feb 20 '12 at 19:30
  • @user1221483 : Do check the image now, is this what you wanted? – nIcE cOw Feb 20 '12 at 19:46
  • Pictures have become smaller, but they are above the line of text http://s017.radikal.ru/i422/1202/88/372b039b4b14.gif The picture on the right, then what should be obtained. The picture on the left - the reality – user1221483 Feb 20 '12 at 19:52
  • @user1221483 : Sorry got you, I just deleted the extra space from your smiley image, you pointed me at, and now see the latest edit regarding that. If you want to remove more space, use any Image Editing software for that. :-) – nIcE cOw Feb 20 '12 at 19:59
  • Thanks, looks like JEditorPane does not suit me. What are the other ways to create a textarea with support for images and flexible settings? – user1221483 Feb 20 '12 at 20:10
  • @user1221483 : Try JTextPane as another option, JTextArea is not the right thing for such things. – nIcE cOw Feb 20 '12 at 20:35
  • @Gagandeep Bali nice, nice +1 – mKorbel Feb 20 '12 at 23:04