Display selected image in a JTextPane

耗尽温柔 提交于 2019-12-11 15:00:06

问题


How can I show an image I have selected from the file chooser in a my textpane (jTextPaneBody)? This is the code I have so far but I don't know what else needs to be added in order to achieve this.

private void jButtonAttachActionPerformed(java.awt.event.ActionEvent evt) {                                              
        JFileChooser jc = new JFileChooser();
        jc.setDialogType(JFileChooser.OPEN_DIALOG);
        jc.showOpenDialog(null);
        File f = jc.getSelectedFile();
    } 

回答1:


Start by taking a look at How to Use Labels and Reading/Loading an Image

BufferedImage img = ImageIO.read(f);
JLabel label = new JLabel(new ImageIcon(img));

Then you can use JTextPane#insertIcon or JTextPane#insertComponent to physically add the image based on your needs



来源:https://stackoverflow.com/questions/28269060/display-selected-image-in-a-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!