It seems that you're using an outdated browser. Some things may not work as they should (or don't work at all).
We suggest you upgrade newer and better browser like: Chrome, Firefox, Internet Explorer or Opera

×
This is getting absolutely absurd! ALL I want to be able to do is use a JTextPane for displaying rich text (RTF) with the ability to cut, copy and paste with the clipboard! That shouldn't be so impossible, should it?

But every single one of the dozens of examples that I've found on the Internet has been flawed in some way or just plain didn't work! It seems that it's fairly easy to format text which is already there, and fairly easy to cut and paste text without preserving formatting, but to do it with formatting? Oh, no!

Anyway, I've gotten to the point where I seem to be able to copy, but it always copies ALL text, rather than just what's selected. If I could either limit it to the selection, or trim it down after the fact, that would be great.

As for pasting, depending on how I do it, it may replace all text or not, but either way the formatting doesn't work. If I absolutely can't insert formatted text by pasting, then at the very least I need to be able to load in ALL text from a file WITH formatting already applied.

The variable called text is just a JTextPane.

So far, here's what I have for copy:

RTFEditorKit rtfKit = new RTFEditorKit();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int start = text.getSelectionStart();
int end = text.getSelectionEnd();
rtfKit.write(outStream, text.getDocument(), start, end - start); // START AND END DON'T SEEM TO WORK!
outStream.flush();
DataHandler handler = new DataHandler(outStream.toByteArray(), rtfKit.getContentType());
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
clip.setContents(handler, null);

Here's the paste:

RTFEditorKit rtfKit = new RTFEditorKit();
int start = text.getSelectionStart();
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
byte[] bytes = clip.getContents(null).toString().getBytes();
ByteArrayInputStream inStream = new ByteArrayInputStream(bytes);
rtfKit.read(inStream, text.getDocument(), start);

I've also tried replacing the fourth line with:

bytes[] bytes = clip.getContents(null).getTransferData(DataFlavor.stringFlavor).toString().getBytes();

In any case it seems to get the bytes of a String which is a representation of the object name/class and stuff, rather than its actual data, which doesn't seem to be readily accessible.

Here's what I have for the file load:

FileReader reader = new FileReader("G:/test doc.rtf");
BufferedReader bufferedReader = new BufferedReader(reader);
String line = "";
String lines = "";
while((line = bufferedReader.readLine()) != null)
{
lines = lines + line;
}
text.setText(lines);

This puts the text into the JTextPane but instead of formatting it, all of the formatting information just gets displayed as plain text.

This is really becoming just about impossible to get ANYTHING to work properly! Could someone PLEASE tell me how to do this or otherwise provide me with some complete source code or a link to it or something? Thank you!

P.S.: Also what about JavaFX TextFlow? I looked into that but it seems to be a Pane (which is sort of a pain, because I don't want to have to format a whole window with it, because I prefer the regular layout, where I can specify coordinates for all of my controls) and I'm not sure whether the text made with that is even editable - is it?
No posts in this topic were marked as the solution yet. If you can help, add your reply
Little bit of advice... there are some people here who seem to be decent developers and willing to help out in all of the dozens of threads you've started like this one.

But maybe you'd be better off posting these kinds of questions on a developer forum. Like Stack Overflow. I'm not saying that you shouldn't post these questions here, just that you'd probably have a lot more people trying to help you out on a developer forum.
You'd think so, but the problem with that is that even I meticulously follow all of their bureaucratic and anal retentive rules, they always manage to nitpick something that they claim I did wrong, so instead of helping me, they just berate me for it and then close the thread so that it can't be replied to, and temporarily ban me from posting anything for the next few days!!! I HATE Stack Overflow!
avatar
HeresMyAccount: This is getting absolutely absurd!
yes