DefaultEditorKit を使用せずにテキストをコピーして HtmlEditorKit に貼り付けたい場合は、独自の貼り付けコードを記述してみてください。
textPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK), "paste");
textPane.getActionMap().put("paste", pasteAction);
class PasteAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
try {
int offset = textPane.getSelectionStart();
Document sd=textPane.getDocument();
String value = getClipboard();
sd.remove(textPane.getSelectionStart(), textPane.getSelectionEnd()-textPane.getSelectionStart());
textPane.getDocument().insertString(offset, value , null);
if (value != null) {
textPane.setCaretPosition(offset + value.length());
}
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
このコードを使用して、クリップボードから純粋なテキストを取得します。
public String getClipboard() throws ClassNotFoundException, UnsupportedFlavorException {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
DataFlavor htmlStringFlavor = new DataFlavor("text/plain; class=java.lang.String");
try {
if (t != null && t.isDataFlavorSupported(htmlStringFlavor)) {
String text = (String) t.getTransferData(htmlStringFlavor);
return text;
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
return null;
}
メニュー項目、ツールバー、またはその他のトリガーがある場合は、「貼り付け」アクションをそれらにバインドすることを忘れないでください。