创建 jEditorPane 的缩略图,文本未呈现
我正在尝试创建一个列表,其中显示 jEditorPane 的缩略图 - 类似于在 powerpoint 中查看每张幻灯片的预览。然而,由于某种原因,我的编辑器窗格中的图像和背景被渲染,但文本却没有渲染。一些示例代码:
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));
}
我发现如果我在 GUI 中使用由 Netbeans 创建的 jEditorPane,则文本渲染确实有效。但是,如果我使用基本构造函数创建一个新的构造函数(如上面的代码所示),则文本不会呈现。这让我觉得布局或一些准备代码中需要包含一些内容才能渲染文本。如有任何帮助,我们将不胜感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信文本组件的渲染比其他组件更复杂,因为您需要在渲染完成之前解析文本并创建文档。我相信编辑器窗格可能使用后台线程或 SwingUtilities.invokeLater() 来帮助完成此过程。
首先,尝试将图像创建代码包装在 SwingUtilities.invokeLater() 中。或者,如果这不起作用,则尝试创建一个单独的线程,该线程会休眠几毫秒,以确保在创建图像之前已解析编辑器窗格文档。
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.
我是这个问题的原发者。我想通了 - 问题是我没有指定组件的大小。这让我觉得自己很愚蠢,因为我没有早点注意到这一点。显然,Netbeans 隐藏了组件的大小调整部分,因此我假设该组件将采用默认组件的首选大小。然而,深入挖掘后,我发现组件的大小为 0,因此除了溢出到组件容器之外的图像之外,实际上根本没有渲染任何内容(可怕!)
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!)