views:

17

answers:

2

I am trying to create a list where thumbnails are shown for a jEditorPane - similar to how in powerpoint you can see a preview of each slide. However, for some reason the images and backgrounds in my editorpane are rendered but the text is not. Some example code:

private void createThumbNailView(javax.swing.event.TreeSelectionEvent evt) {                                    

    JEditorPane test = new JEditorPane();
    JScrollPane jsp = new JScrollPane();

    test.setEditorKit(edkit);
    test.setText("TEST TEXT - THIS WILL NOT BE RENDERED");
    test.setMargin(new java.awt.Insets(30, 30, 30, 60));

    jsp.setViewportView(test);

    BufferedImage bi = new BufferedImage(300,250,BufferedImage.TYPE_INT_RGB);
    test.paint(bi.getGraphics());
    jLabel1.setIcon(new ImageIcon(bi));
} 

I have found that if I use a jEditorPane that is created by Netbeans into the GUI, then the text rendering DOES work. However, if I create a new one (as shown in the code above) with the base constructor, then the text does not render. This makes me think that there is something in the layout or some preparatory code that I need to include for the text to get rendered. Any help at all is appreciated!

A: 

I believe the rendering of text components is more complex then other components since you need to parse the text and create the Document before rendering can be done. I believe the editor pane may use a background Thread or a SwingUtilities.invokeLater() to help with this process.

So first, try wrapping the image creation code in a SwingUtilities.invokeLater(). Or if this doesn't work then try creating a separate Thread that sleeps a few milliseconds to make sure the editor pane Document has been parsed before you create the image.

camickr
I figured this might be the case, until I tried replacing the "test" instance of the JEditorPane with one I placed on the GUI (I am using NetBeans). If I use an instance of JEditorPane created by NetBeans, I can call setText and then paint in the very next line of code and it will be rendered perfectly fine. However, if I create a new instance as in the code above, then the text rendering issue occurs.
evan
A: 

It is I, the original poster of the question. I figured it out - the problem was that I had not specified the size of the component. This makes me feel really foolish that I had not noticed this earlier. Apparently Netbeans hides the sizing portion of components and so I assumed the component would assume the preferred size of the default component. However, After digging a little deeper I found the size of the component was 0 and so there was really nothing being rendered at all, except for the images which were overflowing outside of the component container (scary!)

evan_irl