Posted February 19, 2020
I'm trying to use a JTextPane to do some manipulations to rich (formatted) text in Java Swing. I don't need to insert graphs or images, or anything fancy like that, but I need to be able to set font styles and sizes.
I seems to be able to set the style of the currently selected text, but my problem is that when I try to copy or paste anything using the default methods it only uses plain, unformatted text, even if there was formatting (it's just ignored), so I have to programmatically get around that (why isn't it automated?).
When I try to write code to do it, it doesn't work correctly. For one thing, it always tries to copy all of the text rather than only what's selected, or paste it to replace all text.
I've been experimenting with it so right now it might be slightly messy and there are a few lines that are set as comments so that they won't run at the moment, but if anyone can figure out how to sort out the mess that would be awesome!
Here's what I currently have for my copy function:
public static void copyText(JTextPane text)
{
try
{
RTFEditorKit rtfKit = new RTFEditorKit();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int start = text.getSelectionStart();
int end = text.getSelectionEnd();
System.out.println(start);
System.out.println(end);
//System.out.println("doc type: " + text.getDocument().getClass().toString());
rtfKit.write(outStream, text.getDocument(), start, end - start);// text.getSelectionStart(), text.getSelectionEnd() - text.getSelectionStart());
//rtfKit.write(outStream, text.getDocument().getText(start, end - start), start, end - start);// text.getSelectionStart(), text.getSelectionEnd() - text.getSelectionStart());
//DefaultStyledDocument d = new DefaultStyledDocument("");
outStream.flush();
System.out.println(outStream.toString());
DataHandler handler = new DataHandler(outStream.toByteArray(), rtfKit.getContentType());
//StringSelection selection = new StringSelection(outStream.toString());
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
clip.setContents(handler, null);
//clip.setContents(selection, selection);
////clip.getData(DataFlavor.stringFlavor);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
If I try to make text that looks like:
Here's some text!
In the current state it seems to actually copy the whole thing and even correctly preserve the formatting, except that I can't copy only part of it, and for some unknown reason if I paste it into a different editor it will display the first few characters (up until the "s" in "some", where it becomes italic) with a light blue background, even though I never set a background color. It's almost as though it wants to do that by default and as soon as I change the style it stops doing it. Between that and especially not being able to copy only part of it, I'm having a lot of trouble.
It seems like one thing that I can do is get a substring of the text from the Document object (to try to get the currently selected text), but if I do that then it's not a Document anymore, and doesn't have all of the formatting or compatibility to be input into the rtfKit.write function.
But anyway, here's the actual string that it produces:
{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil Dialog;}
{\colortbl\red0\green0\blue0;\red51\green51\blue51;}
\f1\fs24\i0\b0\cf1 Here's \i some \i0\b text!\b0\par
}
I haven't worked as much on the paste function, but when I paste it just replaces the whole thing with whatever I paste, instead of the selection, and it doesn't format the text at all, but just leaves it plain (except that if the style is different for the first character of whatever is already typed then that style is automatically applied to the whole pasted text). Here's my paste function:
public static void pasteText(JTextPane text)
{
try
{
RTFEditorKit rtfKit = new RTFEditorKit();
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable trans = clip.getContents(null);
if(trans != null && trans.isDataFlavorSupported(DataFlavor.stringFlavor))
{
text.setText((String)trans.getTransferData(DataFlavor.stringFlavor));
//rtfKit.read(trans.toString(), text.getDocument(), text.getSelectionStart());
//text.insertComponent((Component)trans.getTransferData(DataFlavor.stringFlavor));
//text.setText(trans.getTransferData(DataFlavor.stringFlavor).toString());
}
//text.setText(clip.getData(DataFlavor.stringFlavor).toString());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
I've looked all over the Internet, and after putting together everything that I've found, this is unfortunately the best I could do. Does anyone know how to get it the rest of the way there? Thank you!
I seems to be able to set the style of the currently selected text, but my problem is that when I try to copy or paste anything using the default methods it only uses plain, unformatted text, even if there was formatting (it's just ignored), so I have to programmatically get around that (why isn't it automated?).
When I try to write code to do it, it doesn't work correctly. For one thing, it always tries to copy all of the text rather than only what's selected, or paste it to replace all text.
I've been experimenting with it so right now it might be slightly messy and there are a few lines that are set as comments so that they won't run at the moment, but if anyone can figure out how to sort out the mess that would be awesome!
Here's what I currently have for my copy function:
public static void copyText(JTextPane text)
{
try
{
RTFEditorKit rtfKit = new RTFEditorKit();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int start = text.getSelectionStart();
int end = text.getSelectionEnd();
System.out.println(start);
System.out.println(end);
//System.out.println("doc type: " + text.getDocument().getClass().toString());
rtfKit.write(outStream, text.getDocument(), start, end - start);// text.getSelectionStart(), text.getSelectionEnd() - text.getSelectionStart());
//rtfKit.write(outStream, text.getDocument().getText(start, end - start), start, end - start);// text.getSelectionStart(), text.getSelectionEnd() - text.getSelectionStart());
//DefaultStyledDocument d = new DefaultStyledDocument("");
outStream.flush();
System.out.println(outStream.toString());
DataHandler handler = new DataHandler(outStream.toByteArray(), rtfKit.getContentType());
//StringSelection selection = new StringSelection(outStream.toString());
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
clip.setContents(handler, null);
//clip.setContents(selection, selection);
////clip.getData(DataFlavor.stringFlavor);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
If I try to make text that looks like:
Here's some text!
In the current state it seems to actually copy the whole thing and even correctly preserve the formatting, except that I can't copy only part of it, and for some unknown reason if I paste it into a different editor it will display the first few characters (up until the "s" in "some", where it becomes italic) with a light blue background, even though I never set a background color. It's almost as though it wants to do that by default and as soon as I change the style it stops doing it. Between that and especially not being able to copy only part of it, I'm having a lot of trouble.
It seems like one thing that I can do is get a substring of the text from the Document object (to try to get the currently selected text), but if I do that then it's not a Document anymore, and doesn't have all of the formatting or compatibility to be input into the rtfKit.write function.
But anyway, here's the actual string that it produces:
{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil Dialog;}
{\colortbl\red0\green0\blue0;\red51\green51\blue51;}
\f1\fs24\i0\b0\cf1 Here's \i some \i0\b text!\b0\par
}
I haven't worked as much on the paste function, but when I paste it just replaces the whole thing with whatever I paste, instead of the selection, and it doesn't format the text at all, but just leaves it plain (except that if the style is different for the first character of whatever is already typed then that style is automatically applied to the whole pasted text). Here's my paste function:
public static void pasteText(JTextPane text)
{
try
{
RTFEditorKit rtfKit = new RTFEditorKit();
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable trans = clip.getContents(null);
if(trans != null && trans.isDataFlavorSupported(DataFlavor.stringFlavor))
{
text.setText((String)trans.getTransferData(DataFlavor.stringFlavor));
//rtfKit.read(trans.toString(), text.getDocument(), text.getSelectionStart());
//text.insertComponent((Component)trans.getTransferData(DataFlavor.stringFlavor));
//text.setText(trans.getTransferData(DataFlavor.stringFlavor).toString());
}
//text.setText(clip.getData(DataFlavor.stringFlavor).toString());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
I've looked all over the Internet, and after putting together everything that I've found, this is unfortunately the best I could do. Does anyone know how to get it the rest of the way there? Thank you!
No posts in this topic were marked as the solution yet. If you can help, add your reply