Pasting the styled text in to JTextPane is always pasting the text at the end of JTextPane
I am creating a simple notepad in java swing using JTextPane with RTFEditorKit.
I am facing 2 issues.
I am pasting some styled text from external application like Wordpad or MS Office Word like below:

I am placing the cursor at the end of first line > press Enter > then pasting the same styled text again using Ctrl+V keys.
Issue 1: Now the content is not pasted below the first line, it is getting pasted after the last line, like below:

Issue 2: If I copy this styled content from my application and paste it in any other external application like WordPad or MS Office Word, it will be displayed as the plain text.
Why am I facing these issues? Can anyone help.
My code:
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.rtf.RTFEditorKit;
public class MyNotepadMini implements ActionListener {
public JFrame frame;
public JPanel panel;
public JTextPane textPane;
public RTFEditorKit rtf;
public StyleContext styleContext;
public DefaultStyledDocument defaultStyleDoc;
public JScrollPane scrollPane;
public JMenuBar menuBar;
public JMenu fileMenu;
public JMenu editMenu;
public JMenu formatMenu;
public JMenuItem newSubMenu;
public JMenuItem openSubMenu;
public JMenuItem save;
public JMenuItem saveAs;
public JMenuItem cut;
public JMenuItem copy;
public JMenuItem paste;
public JMenuItem selectAll;
public JMenuItem bold;
public JMenuItem italic;
public JMenuItem underline;
public JMenuItem exit;
public JFileChooser fc;
public boolean openFileExtFlag = true;
public boolean saveFileExtFlag = true;
public File openFile;
public File saveFile;
public boolean saveWindowTitle = false;
public boolean openFileFlag;
public boolean saveFileFlag;
public boolean saved = true;
public boolean dontSaveOption;
public BufferedReader br;
public boolean saveForNewOpenExitListener;
public boolean saveAsFlag;
public int returnVal;
public String filePath;
public boolean flagForOpenListener;
public StyledDocument styledDocument;
public Style defaultStyle;
public MyNotepadMini() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
frame = new JFrame("My Notepad");
panel = new JPanel(new BorderLayout());
styledDocument = new DefaultStyledDocument();
rtf = new RTFEditorKit();
textPane = new JTextPane(styledDocument);
textPane.setEditorKit(rtf);
styleContext = new StyleContext();
scrollPane = new JScrollPane();
scrollPane.getViewport().add(textPane);
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
newSubMenu = new JMenuItem("New");
newSubMenu.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
openSubMenu = new JMenuItem("Open...");
openSubMenu.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
save = new JMenuItem("Save");
save.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
cut = new JMenuItem("Cut");
cut.setAccelerator(KeyStroke.getKeyStroke('X', CTRL_DOWN_MASK));
copy = new JMenuItem("Copy");
copy.setAccelerator(KeyStroke.getKeyStroke('C', CTRL_DOWN_MASK));
paste = new JMenuItem("Paste");
paste.setAccelerator(KeyStroke.getKeyStroke('V', CTRL_DOWN_MASK));
selectAll = new JMenuItem("Select All");
selectAll.setAccelerator(KeyStroke.getKeyStroke('A', CTRL_DOWN_MASK));
exit = new JMenuItem("Exit");
exit.setMnemonic(KeyEvent.VK_X);
formatMenu = new JMenu("Format");
formatMenu.setMnemonic(KeyEvent.VK_F);
bold = new JMenuItem(new StyledEditorKit.BoldAction());
bold.setText("Bold");
bold.setMnemonic(KeyEvent.VK_B);
bold.setAccelerator(KeyStroke.getKeyStroke('B', CTRL_DOWN_MASK));
italic = new JMenuItem(new StyledEditorKit.ItalicAction());
italic.setText("Italic");
italic.setMnemonic(KeyEvent.VK_I);
italic.setAccelerator(KeyStroke.getKeyStroke('I', CTRL_DOWN_MASK));
underline = new JMenuItem(new StyledEditorKit.UnderlineAction());
underline.setText("Underline");
underline.setMnemonic(KeyEvent.VK_U);
underline.setAccelerator(KeyStroke.getKeyStroke('U', CTRL_DOWN_MASK));
defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
exit.addActionListener(this);
newSubMenu.addActionListener(this);
editMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent event) {
if (textPane.getSelectionStart() == textPane.getSelectionEnd()) {
cut.setEnabled(false);
copy.setEnabled(false);
} else {
cut.setEnabled(true);
copy.setEnabled(true);
}
}
public void menuDeselected(MenuEvent event) {
}
public void menuCanceled(MenuEvent event) {
}
});
scrollPane.setPreferredSize(new Dimension(700,500));
fileMenu.add(newSubMenu);
fileMenu.add(openSubMenu);
fileMenu.add(save);
fileMenu.add(exit);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.addSeparator();
editMenu.add(selectAll);
formatMenu.add(bold);
formatMenu.add(italic);
formatMenu.add(underline);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(formatMenu);
textPane.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(new JLabel(" "), BorderLayout.EAST);
panel.add(new JLabel(" "), BorderLayout.WEST);
panel.add(new JLabel(" "), BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
textPane.requestFocus();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args) {
new MyNotepadMini();
}
public void actionPerformed(ActionEvent ae) {
if ((ae.getSource() == cut)) {
textPane.cut();
textPane.requestFocus();
} else if ((ae.getSource() == copy)) {
textPane.copy();
textPane.requestFocus();
} else if ((ae.getSource() == paste)) {
textPane.paste();
textPane.requestFocus();
} else if (ae.getSource() == selectAll) {
textPane.selectAll();
} else if ((ae.getSource() == exit)){
System.exit(0);
} else if ((ae.getSource() == newSubMenu)) {
textPane.setText("");
textPane.setStyledDocument(styledDocument = new DefaultStyledDocument());
MutableAttributeSet mas = textPane.getInputAttributes();
mas.removeAttributes(mas);
textPane.requestFocus();
}
}
}
java swing jtextpane styleddocument
add a comment |
I am creating a simple notepad in java swing using JTextPane with RTFEditorKit.
I am facing 2 issues.
I am pasting some styled text from external application like Wordpad or MS Office Word like below:

I am placing the cursor at the end of first line > press Enter > then pasting the same styled text again using Ctrl+V keys.
Issue 1: Now the content is not pasted below the first line, it is getting pasted after the last line, like below:

Issue 2: If I copy this styled content from my application and paste it in any other external application like WordPad or MS Office Word, it will be displayed as the plain text.
Why am I facing these issues? Can anyone help.
My code:
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.rtf.RTFEditorKit;
public class MyNotepadMini implements ActionListener {
public JFrame frame;
public JPanel panel;
public JTextPane textPane;
public RTFEditorKit rtf;
public StyleContext styleContext;
public DefaultStyledDocument defaultStyleDoc;
public JScrollPane scrollPane;
public JMenuBar menuBar;
public JMenu fileMenu;
public JMenu editMenu;
public JMenu formatMenu;
public JMenuItem newSubMenu;
public JMenuItem openSubMenu;
public JMenuItem save;
public JMenuItem saveAs;
public JMenuItem cut;
public JMenuItem copy;
public JMenuItem paste;
public JMenuItem selectAll;
public JMenuItem bold;
public JMenuItem italic;
public JMenuItem underline;
public JMenuItem exit;
public JFileChooser fc;
public boolean openFileExtFlag = true;
public boolean saveFileExtFlag = true;
public File openFile;
public File saveFile;
public boolean saveWindowTitle = false;
public boolean openFileFlag;
public boolean saveFileFlag;
public boolean saved = true;
public boolean dontSaveOption;
public BufferedReader br;
public boolean saveForNewOpenExitListener;
public boolean saveAsFlag;
public int returnVal;
public String filePath;
public boolean flagForOpenListener;
public StyledDocument styledDocument;
public Style defaultStyle;
public MyNotepadMini() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
frame = new JFrame("My Notepad");
panel = new JPanel(new BorderLayout());
styledDocument = new DefaultStyledDocument();
rtf = new RTFEditorKit();
textPane = new JTextPane(styledDocument);
textPane.setEditorKit(rtf);
styleContext = new StyleContext();
scrollPane = new JScrollPane();
scrollPane.getViewport().add(textPane);
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
newSubMenu = new JMenuItem("New");
newSubMenu.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
openSubMenu = new JMenuItem("Open...");
openSubMenu.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
save = new JMenuItem("Save");
save.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
cut = new JMenuItem("Cut");
cut.setAccelerator(KeyStroke.getKeyStroke('X', CTRL_DOWN_MASK));
copy = new JMenuItem("Copy");
copy.setAccelerator(KeyStroke.getKeyStroke('C', CTRL_DOWN_MASK));
paste = new JMenuItem("Paste");
paste.setAccelerator(KeyStroke.getKeyStroke('V', CTRL_DOWN_MASK));
selectAll = new JMenuItem("Select All");
selectAll.setAccelerator(KeyStroke.getKeyStroke('A', CTRL_DOWN_MASK));
exit = new JMenuItem("Exit");
exit.setMnemonic(KeyEvent.VK_X);
formatMenu = new JMenu("Format");
formatMenu.setMnemonic(KeyEvent.VK_F);
bold = new JMenuItem(new StyledEditorKit.BoldAction());
bold.setText("Bold");
bold.setMnemonic(KeyEvent.VK_B);
bold.setAccelerator(KeyStroke.getKeyStroke('B', CTRL_DOWN_MASK));
italic = new JMenuItem(new StyledEditorKit.ItalicAction());
italic.setText("Italic");
italic.setMnemonic(KeyEvent.VK_I);
italic.setAccelerator(KeyStroke.getKeyStroke('I', CTRL_DOWN_MASK));
underline = new JMenuItem(new StyledEditorKit.UnderlineAction());
underline.setText("Underline");
underline.setMnemonic(KeyEvent.VK_U);
underline.setAccelerator(KeyStroke.getKeyStroke('U', CTRL_DOWN_MASK));
defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
exit.addActionListener(this);
newSubMenu.addActionListener(this);
editMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent event) {
if (textPane.getSelectionStart() == textPane.getSelectionEnd()) {
cut.setEnabled(false);
copy.setEnabled(false);
} else {
cut.setEnabled(true);
copy.setEnabled(true);
}
}
public void menuDeselected(MenuEvent event) {
}
public void menuCanceled(MenuEvent event) {
}
});
scrollPane.setPreferredSize(new Dimension(700,500));
fileMenu.add(newSubMenu);
fileMenu.add(openSubMenu);
fileMenu.add(save);
fileMenu.add(exit);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.addSeparator();
editMenu.add(selectAll);
formatMenu.add(bold);
formatMenu.add(italic);
formatMenu.add(underline);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(formatMenu);
textPane.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(new JLabel(" "), BorderLayout.EAST);
panel.add(new JLabel(" "), BorderLayout.WEST);
panel.add(new JLabel(" "), BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
textPane.requestFocus();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args) {
new MyNotepadMini();
}
public void actionPerformed(ActionEvent ae) {
if ((ae.getSource() == cut)) {
textPane.cut();
textPane.requestFocus();
} else if ((ae.getSource() == copy)) {
textPane.copy();
textPane.requestFocus();
} else if ((ae.getSource() == paste)) {
textPane.paste();
textPane.requestFocus();
} else if (ae.getSource() == selectAll) {
textPane.selectAll();
} else if ((ae.getSource() == exit)){
System.exit(0);
} else if ((ae.getSource() == newSubMenu)) {
textPane.setText("");
textPane.setStyledDocument(styledDocument = new DefaultStyledDocument());
MutableAttributeSet mas = textPane.getInputAttributes();
mas.removeAttributes(mas);
textPane.requestFocus();
}
}
}
java swing jtextpane styleddocument
add a comment |
I am creating a simple notepad in java swing using JTextPane with RTFEditorKit.
I am facing 2 issues.
I am pasting some styled text from external application like Wordpad or MS Office Word like below:

I am placing the cursor at the end of first line > press Enter > then pasting the same styled text again using Ctrl+V keys.
Issue 1: Now the content is not pasted below the first line, it is getting pasted after the last line, like below:

Issue 2: If I copy this styled content from my application and paste it in any other external application like WordPad or MS Office Word, it will be displayed as the plain text.
Why am I facing these issues? Can anyone help.
My code:
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.rtf.RTFEditorKit;
public class MyNotepadMini implements ActionListener {
public JFrame frame;
public JPanel panel;
public JTextPane textPane;
public RTFEditorKit rtf;
public StyleContext styleContext;
public DefaultStyledDocument defaultStyleDoc;
public JScrollPane scrollPane;
public JMenuBar menuBar;
public JMenu fileMenu;
public JMenu editMenu;
public JMenu formatMenu;
public JMenuItem newSubMenu;
public JMenuItem openSubMenu;
public JMenuItem save;
public JMenuItem saveAs;
public JMenuItem cut;
public JMenuItem copy;
public JMenuItem paste;
public JMenuItem selectAll;
public JMenuItem bold;
public JMenuItem italic;
public JMenuItem underline;
public JMenuItem exit;
public JFileChooser fc;
public boolean openFileExtFlag = true;
public boolean saveFileExtFlag = true;
public File openFile;
public File saveFile;
public boolean saveWindowTitle = false;
public boolean openFileFlag;
public boolean saveFileFlag;
public boolean saved = true;
public boolean dontSaveOption;
public BufferedReader br;
public boolean saveForNewOpenExitListener;
public boolean saveAsFlag;
public int returnVal;
public String filePath;
public boolean flagForOpenListener;
public StyledDocument styledDocument;
public Style defaultStyle;
public MyNotepadMini() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
frame = new JFrame("My Notepad");
panel = new JPanel(new BorderLayout());
styledDocument = new DefaultStyledDocument();
rtf = new RTFEditorKit();
textPane = new JTextPane(styledDocument);
textPane.setEditorKit(rtf);
styleContext = new StyleContext();
scrollPane = new JScrollPane();
scrollPane.getViewport().add(textPane);
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
newSubMenu = new JMenuItem("New");
newSubMenu.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
openSubMenu = new JMenuItem("Open...");
openSubMenu.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
save = new JMenuItem("Save");
save.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
cut = new JMenuItem("Cut");
cut.setAccelerator(KeyStroke.getKeyStroke('X', CTRL_DOWN_MASK));
copy = new JMenuItem("Copy");
copy.setAccelerator(KeyStroke.getKeyStroke('C', CTRL_DOWN_MASK));
paste = new JMenuItem("Paste");
paste.setAccelerator(KeyStroke.getKeyStroke('V', CTRL_DOWN_MASK));
selectAll = new JMenuItem("Select All");
selectAll.setAccelerator(KeyStroke.getKeyStroke('A', CTRL_DOWN_MASK));
exit = new JMenuItem("Exit");
exit.setMnemonic(KeyEvent.VK_X);
formatMenu = new JMenu("Format");
formatMenu.setMnemonic(KeyEvent.VK_F);
bold = new JMenuItem(new StyledEditorKit.BoldAction());
bold.setText("Bold");
bold.setMnemonic(KeyEvent.VK_B);
bold.setAccelerator(KeyStroke.getKeyStroke('B', CTRL_DOWN_MASK));
italic = new JMenuItem(new StyledEditorKit.ItalicAction());
italic.setText("Italic");
italic.setMnemonic(KeyEvent.VK_I);
italic.setAccelerator(KeyStroke.getKeyStroke('I', CTRL_DOWN_MASK));
underline = new JMenuItem(new StyledEditorKit.UnderlineAction());
underline.setText("Underline");
underline.setMnemonic(KeyEvent.VK_U);
underline.setAccelerator(KeyStroke.getKeyStroke('U', CTRL_DOWN_MASK));
defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
exit.addActionListener(this);
newSubMenu.addActionListener(this);
editMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent event) {
if (textPane.getSelectionStart() == textPane.getSelectionEnd()) {
cut.setEnabled(false);
copy.setEnabled(false);
} else {
cut.setEnabled(true);
copy.setEnabled(true);
}
}
public void menuDeselected(MenuEvent event) {
}
public void menuCanceled(MenuEvent event) {
}
});
scrollPane.setPreferredSize(new Dimension(700,500));
fileMenu.add(newSubMenu);
fileMenu.add(openSubMenu);
fileMenu.add(save);
fileMenu.add(exit);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.addSeparator();
editMenu.add(selectAll);
formatMenu.add(bold);
formatMenu.add(italic);
formatMenu.add(underline);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(formatMenu);
textPane.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(new JLabel(" "), BorderLayout.EAST);
panel.add(new JLabel(" "), BorderLayout.WEST);
panel.add(new JLabel(" "), BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
textPane.requestFocus();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args) {
new MyNotepadMini();
}
public void actionPerformed(ActionEvent ae) {
if ((ae.getSource() == cut)) {
textPane.cut();
textPane.requestFocus();
} else if ((ae.getSource() == copy)) {
textPane.copy();
textPane.requestFocus();
} else if ((ae.getSource() == paste)) {
textPane.paste();
textPane.requestFocus();
} else if (ae.getSource() == selectAll) {
textPane.selectAll();
} else if ((ae.getSource() == exit)){
System.exit(0);
} else if ((ae.getSource() == newSubMenu)) {
textPane.setText("");
textPane.setStyledDocument(styledDocument = new DefaultStyledDocument());
MutableAttributeSet mas = textPane.getInputAttributes();
mas.removeAttributes(mas);
textPane.requestFocus();
}
}
}
java swing jtextpane styleddocument
I am creating a simple notepad in java swing using JTextPane with RTFEditorKit.
I am facing 2 issues.
I am pasting some styled text from external application like Wordpad or MS Office Word like below:

I am placing the cursor at the end of first line > press Enter > then pasting the same styled text again using Ctrl+V keys.
Issue 1: Now the content is not pasted below the first line, it is getting pasted after the last line, like below:

Issue 2: If I copy this styled content from my application and paste it in any other external application like WordPad or MS Office Word, it will be displayed as the plain text.
Why am I facing these issues? Can anyone help.
My code:
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.rtf.RTFEditorKit;
public class MyNotepadMini implements ActionListener {
public JFrame frame;
public JPanel panel;
public JTextPane textPane;
public RTFEditorKit rtf;
public StyleContext styleContext;
public DefaultStyledDocument defaultStyleDoc;
public JScrollPane scrollPane;
public JMenuBar menuBar;
public JMenu fileMenu;
public JMenu editMenu;
public JMenu formatMenu;
public JMenuItem newSubMenu;
public JMenuItem openSubMenu;
public JMenuItem save;
public JMenuItem saveAs;
public JMenuItem cut;
public JMenuItem copy;
public JMenuItem paste;
public JMenuItem selectAll;
public JMenuItem bold;
public JMenuItem italic;
public JMenuItem underline;
public JMenuItem exit;
public JFileChooser fc;
public boolean openFileExtFlag = true;
public boolean saveFileExtFlag = true;
public File openFile;
public File saveFile;
public boolean saveWindowTitle = false;
public boolean openFileFlag;
public boolean saveFileFlag;
public boolean saved = true;
public boolean dontSaveOption;
public BufferedReader br;
public boolean saveForNewOpenExitListener;
public boolean saveAsFlag;
public int returnVal;
public String filePath;
public boolean flagForOpenListener;
public StyledDocument styledDocument;
public Style defaultStyle;
public MyNotepadMini() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
frame = new JFrame("My Notepad");
panel = new JPanel(new BorderLayout());
styledDocument = new DefaultStyledDocument();
rtf = new RTFEditorKit();
textPane = new JTextPane(styledDocument);
textPane.setEditorKit(rtf);
styleContext = new StyleContext();
scrollPane = new JScrollPane();
scrollPane.getViewport().add(textPane);
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
newSubMenu = new JMenuItem("New");
newSubMenu.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
openSubMenu = new JMenuItem("Open...");
openSubMenu.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
save = new JMenuItem("Save");
save.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
cut = new JMenuItem("Cut");
cut.setAccelerator(KeyStroke.getKeyStroke('X', CTRL_DOWN_MASK));
copy = new JMenuItem("Copy");
copy.setAccelerator(KeyStroke.getKeyStroke('C', CTRL_DOWN_MASK));
paste = new JMenuItem("Paste");
paste.setAccelerator(KeyStroke.getKeyStroke('V', CTRL_DOWN_MASK));
selectAll = new JMenuItem("Select All");
selectAll.setAccelerator(KeyStroke.getKeyStroke('A', CTRL_DOWN_MASK));
exit = new JMenuItem("Exit");
exit.setMnemonic(KeyEvent.VK_X);
formatMenu = new JMenu("Format");
formatMenu.setMnemonic(KeyEvent.VK_F);
bold = new JMenuItem(new StyledEditorKit.BoldAction());
bold.setText("Bold");
bold.setMnemonic(KeyEvent.VK_B);
bold.setAccelerator(KeyStroke.getKeyStroke('B', CTRL_DOWN_MASK));
italic = new JMenuItem(new StyledEditorKit.ItalicAction());
italic.setText("Italic");
italic.setMnemonic(KeyEvent.VK_I);
italic.setAccelerator(KeyStroke.getKeyStroke('I', CTRL_DOWN_MASK));
underline = new JMenuItem(new StyledEditorKit.UnderlineAction());
underline.setText("Underline");
underline.setMnemonic(KeyEvent.VK_U);
underline.setAccelerator(KeyStroke.getKeyStroke('U', CTRL_DOWN_MASK));
defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
exit.addActionListener(this);
newSubMenu.addActionListener(this);
editMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent event) {
if (textPane.getSelectionStart() == textPane.getSelectionEnd()) {
cut.setEnabled(false);
copy.setEnabled(false);
} else {
cut.setEnabled(true);
copy.setEnabled(true);
}
}
public void menuDeselected(MenuEvent event) {
}
public void menuCanceled(MenuEvent event) {
}
});
scrollPane.setPreferredSize(new Dimension(700,500));
fileMenu.add(newSubMenu);
fileMenu.add(openSubMenu);
fileMenu.add(save);
fileMenu.add(exit);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.addSeparator();
editMenu.add(selectAll);
formatMenu.add(bold);
formatMenu.add(italic);
formatMenu.add(underline);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(formatMenu);
textPane.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(new JLabel(" "), BorderLayout.EAST);
panel.add(new JLabel(" "), BorderLayout.WEST);
panel.add(new JLabel(" "), BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
textPane.requestFocus();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args) {
new MyNotepadMini();
}
public void actionPerformed(ActionEvent ae) {
if ((ae.getSource() == cut)) {
textPane.cut();
textPane.requestFocus();
} else if ((ae.getSource() == copy)) {
textPane.copy();
textPane.requestFocus();
} else if ((ae.getSource() == paste)) {
textPane.paste();
textPane.requestFocus();
} else if (ae.getSource() == selectAll) {
textPane.selectAll();
} else if ((ae.getSource() == exit)){
System.exit(0);
} else if ((ae.getSource() == newSubMenu)) {
textPane.setText("");
textPane.setStyledDocument(styledDocument = new DefaultStyledDocument());
MutableAttributeSet mas = textPane.getInputAttributes();
mas.removeAttributes(mas);
textPane.requestFocus();
}
}
}
java swing jtextpane styleddocument
java swing jtextpane styleddocument
edited Nov 17 '18 at 8:48
AbiSaran
asked Nov 17 '18 at 7:10
AbiSaranAbiSaran
3716
3716
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349054%2fpasting-the-styled-text-in-to-jtextpane-is-always-pasting-the-text-at-the-end-of%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349054%2fpasting-the-styled-text-in-to-jtextpane-is-always-pasting-the-text-at-the-end-of%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown