How to copy an image in JTextPane java?

蹲街弑〆低调 提交于 2019-12-10 18:26:46

问题


I want to know how to copy an image and text in JTextPane. When I use this code, it copies just text but I want to copy text and image. How can be done this?

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
  *
     * @author admin
                     */
public class Main extends JFrame implements KeyListener, ActionListener{
public static JTextPane textPane;
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
                JFrame Frame = new Main();
                Frame.setVisible(true);
                Frame.setSize(400, 400);





}
public Main()
{
    JMenuBar mb = new JMenuBar();
    setJMenuBar(mb);

    JMenu menu = new JMenu("File");
    JMenuItem mi = new JMenuItem("select all");
    mi.addActionListener(this);
    menu.setMnemonic(KeyEvent.VK_F);
    menu.add(mi);
    mi = new JMenuItem("copy");
    mi.addActionListener(this);
    menu.add(mi);
    mi = new JMenuItem("Exit");
    mi.addActionListener(this);
    menu.add(mi);
     mi = new JMenuItem("insert image");
    mi.addActionListener(this);
    menu.add(mi);
    mb.add(menu);
    textPane = new JTextPane();

    JScrollPane scrollPane = new JScrollPane(textPane);
    getContentPane().add(scrollPane);


}

public void keyTyped(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void keyPressed(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void keyReleased(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void actionPerformed(ActionEvent e) {
    String cmd=e.getActionCommand();
    if ("Exit".equals(cmd)) {
        System.exit(0);
    } else if ("select all".equals(cmd)) {
        textPane.selectAll();
    } 
    else if ("copy".equals(cmd)) {
      textPane.copy();

    }
    else if("insert image".equals(cmd))
    {
        try {
            JFileChooser file = new JFileChooser();
            file.showOpenDialog(null);
            File selFile = file.getSelectedFile();
            Image img = ImageIO.read(selFile);
            textPane.insertIcon(new ImageIcon(img));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
 }

}

回答1:


I'm afraid there is no simple way to do this. All the default EditorKits (StyledEditorKit, HTMLEditorKit, RTFEditorKit) don't support images copying.

The closest one is HTMLEditorKit but it will generate HTML with links to images.

You can implement your own Reader/Writer. See http://java-sl.com/editor_kit_tutorial.html chapter about reader and writer.




回答2:


Unfortunately there is no way to do this. The field is called a JTextPane for a reason. It cannot handle images.




回答3:


I think there is a way: use FileInputStream and FileOutputStream if you want to open an rtf document in jtextpane if you want to load it.Since it is a stream of byte it will try to load it byte by byte.

But there is hardly any way to copy it.



来源:https://stackoverflow.com/questions/6208530/how-to-copy-an-image-in-jtextpane-java

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