Java 内联复制粘贴窗格HTML
我正在拼命尝试在HTML模式下的JTextPane中实现定制的复制/粘贴。 大部分工作正常,我使用EditorKit.write()获取html内容,使用EditorKit.read()粘贴html内容。完美的世界 但是,当我在编辑器中有:Java 内联复制粘贴窗格HTML,java,swing,copy-paste,jtextpane,dom,Java,Swing,Copy Paste,Jtextpane,Dom,我正在拼命尝试在HTML模式下的JTextPane中实现定制的复制/粘贴。 大部分工作正常,我使用EditorKit.write()获取html内容,使用EditorKit.read()粘贴html内容。完美的世界 但是,当我在编辑器中有:test时,我会尝试复制“es”以获得 teest,我获得 <p>tes</p> <p>es</p> <p>t</p> 我无法显示结果:EditorKit.write尝试自行修复html
test
时,我会尝试复制“es”以获得
teest
,我获得
<p>tes</p>
<p>es</p>
<p>t</p>
我无法显示结果:EditorKit.write尝试自行修复html。但是HTMLDocument是完全混乱的
供您尝试:
public class Test {
private static JTextPane editor = new Editor();
private static JMenuBar menu = new Menu();
private static String clipboard = "";
private static Action copy = new Copy();
private static Action paste = new Paste();
public static void main(String[] args) {
JFrame f = new JFrame();
f.setContentPane(editor);
f.setJMenuBar(menu);
f.setSize(600, 400);
f.setVisible(true);
}
public static class Editor extends JTextPane {
public Editor() {
this.setDocument(new HTMLDocument());
this.setEditorKit(new HTMLEditorKit());
}
}
public static class Menu extends JMenuBar {
public Menu() {
add(new JButton(copy));
add(new JButton(paste));
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK), "copy");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK), "paste");
getActionMap().put("copy", copy);
getActionMap().put("paste", paste);
}
}
public static class Copy extends AbstractAction {
public Copy() {super("copy");}
@Override
public void actionPerformed(ActionEvent e) {
StringWriter w = new StringWriter();
try {
editor.getEditorKit().write(w, editor.getDocument(), editor.getCaretPosition(), editor.getSelectedText().length());
} catch (Exception ex) {Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);}
clipboard = w.toString();
}
}
public static class Paste extends AbstractAction {
public Paste() {super("paste");}
@Override
public void actionPerformed(ActionEvent e) {
try {
editor.getEditorKit().read(new StringReader(clipboard), editor.getDocument(), editor.getCaretPosition());
} catch (Exception ex) {Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);}
}
}
}
对不起,我太久了。我接受任何帮助。恐怕没有捷径。粘贴时,您希望保留原始段落并避免创建新段落,对吗?问题在于当前段落和复制的段落可能具有不同的属性。例如,当前为左对齐,但复制的当前为右对齐 如何解决这个案件?为了简化此工具包,只需创建
元素
您可以尝试从剪贴板内容创建一个独立的HTMLDocument,并遍历文档结构,提取元素(段落和文本)并将其插入原始文档中。恐怕没有简单的方法。粘贴时,您希望保留原始段落并避免创建新段落,对吗?问题在于当前段落和复制的段落可能具有不同的属性。例如,当前为左对齐,但复制的当前为右对齐 如何解决这个案件?为了简化此工具包,只需创建
元素
您可以尝试从剪贴板内容创建一个独立的HTMLDocument,并遍历文档结构提取元素(段落和文本)并将它们插入原始文档。对于其他读者,我用一个非常简单的技巧解决了这个问题。我只是在需要时删除了粘贴文本前后添加的
\n
public static void copyContent(JTextPane jtp, String text, int offset) {
try {
boolean start = offset>0 ? !jtp.getText(offset-1, 1).equals("\n") : false;
Position p = jtp.getDocument().createPosition(offset);
new HTMLEditorKit().read(new StringReader(html), jtp.getHTMLdoc(), offset);
if(start) {jtp.getDocument().remove(offset, 1);}
if(offset>0) {jtp.getDocument().remove(p.getOffset()-1, 1);}
} catch (IOException | BadLocationException ex) {
Logger.getLogger(EditeurIO.class.getName()).log(Level.SEVERE, null, ex);
}
}
对于更多的读者,我用一个非常简单的技巧解决了这个问题。我只是在需要时删除了粘贴文本前后添加的
\n
public static void copyContent(JTextPane jtp, String text, int offset) {
try {
boolean start = offset>0 ? !jtp.getText(offset-1, 1).equals("\n") : false;
Position p = jtp.getDocument().createPosition(offset);
new HTMLEditorKit().read(new StringReader(html), jtp.getHTMLdoc(), offset);
if(start) {jtp.getDocument().remove(offset, 1);}
if(offset>0) {jtp.getDocument().remove(p.getOffset()-1, 1);}
} catch (IOException | BadLocationException ex) {
Logger.getLogger(EditeurIO.class.getName()).log(Level.SEVERE, null, ex);
}
}
为了更快地获得更好的帮助,请发布一个。为了更快地获得更好的帮助,请发布一个。您如何迭代它?您是否介意包含一些示例代码,因为这将非常有帮助。@StephaneGrenier您可以尝试合并2个文档的示例作为基础。在本例中,我希望有一种方法找到与选择相对应的原始html,这意味着提取整个html,并且能够找到“SelectionStart”和“selectionEnd”位置。有什么方法可以做到这一点吗?您可以使用kit的write方法传递基于选择开始/结束的值show您是否遍历它?您是否介意包含一些示例代码,因为这将非常有帮助。@StephaneGrenier您可以尝试合并2个文档的示例作为基础。在本例中,我希望有一种方法找到与选择相对应的原始html,这意味着提取整个html,并且能够找到“SelectionStart”和“selectionEnd”位置。有什么方法可以做到这一点吗?您可以使用kit的write方法传递基于选择开始/结束的值
<p>mon</p>
<p>beau sapin</p>
<p>roi des forêts</p>
<p>que</p>
<p>beau sapin</p>
<p>roi des forêts</p>
<p>que j'aime ta verdure</p>
private static void insertHTMLContent(JMathTextPane jtp, String html, int offset) {
Document doc = Jsoup.parse(html);
Elements elts = doc.body().children();
//unwrap the last and first element
if(elts.size()>2) { elts.last().unwrap(); }
if(elts.size()>=1) { elts.first().unwrap(); }
//We add a fake DIV element and remove it just at the next line
editorKit.insertHTML(jtp.htmlDoc, offset, "<div id='copie'>"+doc.body().html()+"</div>", 0, 0, HTML.Tag.DIV);
jtp.getHTMLdoc().setOuterHTML(jtp.getHTMLdoc().getElement("copie"),doc.body().html());
}
public class Test {
private static JTextPane editor = new Editor();
private static JMenuBar menu = new Menu();
private static String clipboard = "";
private static Action copy = new Copy();
private static Action paste = new Paste();
public static void main(String[] args) {
JFrame f = new JFrame();
f.setContentPane(editor);
f.setJMenuBar(menu);
f.setSize(600, 400);
f.setVisible(true);
}
public static class Editor extends JTextPane {
public Editor() {
this.setDocument(new HTMLDocument());
this.setEditorKit(new HTMLEditorKit());
}
}
public static class Menu extends JMenuBar {
public Menu() {
add(new JButton(copy));
add(new JButton(paste));
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK), "copy");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK), "paste");
getActionMap().put("copy", copy);
getActionMap().put("paste", paste);
}
}
public static class Copy extends AbstractAction {
public Copy() {super("copy");}
@Override
public void actionPerformed(ActionEvent e) {
StringWriter w = new StringWriter();
try {
editor.getEditorKit().write(w, editor.getDocument(), editor.getCaretPosition(), editor.getSelectedText().length());
} catch (Exception ex) {Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);}
clipboard = w.toString();
}
}
public static class Paste extends AbstractAction {
public Paste() {super("paste");}
@Override
public void actionPerformed(ActionEvent e) {
try {
editor.getEditorKit().read(new StringReader(clipboard), editor.getDocument(), editor.getCaretPosition());
} catch (Exception ex) {Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);}
}
}
}
public static void copyContent(JTextPane jtp, String text, int offset) {
try {
boolean start = offset>0 ? !jtp.getText(offset-1, 1).equals("\n") : false;
Position p = jtp.getDocument().createPosition(offset);
new HTMLEditorKit().read(new StringReader(html), jtp.getHTMLdoc(), offset);
if(start) {jtp.getDocument().remove(offset, 1);}
if(offset>0) {jtp.getDocument().remove(p.getOffset()-1, 1);}
} catch (IOException | BadLocationException ex) {
Logger.getLogger(EditeurIO.class.getName()).log(Level.SEVERE, null, ex);
}
}