HTML 모드의 JTextPane에 사용자 정의 된 복사/붙여 넣기를 구현하려고 노력 중입니다. 대부분 잘 작동하고, EditorKit.write()를 사용하여 html 콘텐츠를 가져 와서 editorKit.read()를 사용하여 붙여 넣습니다. 완벽한 세상.인라인 복사 붙여 넣기 JEditorPane HTML
하지만, 내가 가진 : <p> test </p>
내 편집기에서 내가 <p> tesest </p>
을 얻기 위해 "ES"를 복사하려고, 내가, 내가 붙여 넣을 수있는 방법을 알아 내기 위해 노력하고 있음을 알고 대신
<p>tes</p>
<p>es</p>
<p>t</p>
획득 "인라인 (inline)"부분은 인라인 (inline)이고, 블록 (block) 부분은 복사 중에 블록 된 부분입니다.
<p>mon beau sapin</p>
<p>roi des forêts</p>
<p>que j'aime ta verdure</p>
을 그리고 복사 할 경우 : 일반적으로,
나는 경우beau sapin</p>
<p>roi des forêts</p>
<p>que
을 그리고 "월"후 붙여 넣기, 내가 기대 :
<p>mon beau sapin</p>
<p>roi des forêts</p>
<p>que beau sapin</p>
<p>roi des forêts</p>
<p>que j'aime ta verdure</p>
그리고 취득 대신 :
<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>
editorKit.insertHTML (하지만 어떤 종류의 태그를 넣어야합니까?)를 사용하여 첫 번째 줄과 마지막 줄 (EditorKit.read를 단독으로 다시 추가)의 <p></p>
을 제거하는 등 다양한 방법을 시도했습니다. 줄 단위로 줄을 삽입합니다. 대부분의 경우, 나는 다른 p
안에 p
을 얻습니다.
그러나 htmlDocument에서 원하는 것을 쓸 수 없다는 것이 진짜 문제입니다. 어떻게 특정 위치에 sapin</p> <p>roi
을 쓸 수 있습니까? EditorKit.read? 추가 될 것입니다 <p>sapin</p> <p>roi</p>
Editorkit.insertHTML? 내가 당신에게 결과를 표시 할 수 없습니다
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());
}
: 난 당신에게 내 마지막 시도를 보여
... 정확한 랩핑에 태그를 추가 할 필요가 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);}
}
}
}
내가 길었다 죄송합니다. 나는 어떤 도움도 받아 들인다.
더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. –