创建 jEditorPane 的缩略图,文本未呈现

发布于 2024-09-17 10:19:43 字数 822 浏览 4 评论 0原文

我正在尝试创建一个列表,其中显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2024-09-24 10:19:43

我相信文本组件的渲染比其他组件更复杂,因为您需要在渲染完成之前解析文本并创建文档。我相信编辑器窗格可能使用后台线程或 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.

左耳近心 2024-09-24 10:19:43

我是这个问题的原发者。我想通了 - 问题是我没有指定组件的大小。这让我觉得自己很愚蠢,因为我没有早点注意到这一点。显然,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!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文