AWT TextArea seems to count cr-lf sequence as one character - java

I have this problem:
The text "ABCD\r\nEFGHJ" loaded from a file is matched with java regex "EFGH". Matcher object of course says start of the matched string is in position 6. The matcher counts \r \n as two positions.
I put the original text in a AWT TextArea Component and then call select(6,10) to highlight the area which was matched. Guess what... it starts highlighting from 'F' letter. One position forward than it should...
If more than 1 pair of crlf precedes matched area then highlighting moves even more forward than it should.
Anyone has any simple solution?

Simple solution: remove all \r from the text... :-P
Not as stupid as it sounds, unless you have inconsistent end of lines (it can happen) and want to keep them unchanged... And that's probably what the component does anyway.

I cant mess with the text because it is protocol data and \r and \n characters have semantics that dont have to do with display or line separation. I just want a component that will treat each one input character separately and treat it as one displayed and counted, no matter how it is displayed.

If the \r\n are consistent, you can remove the \r's before running the regex, then replace them before handing off to whatever is next. Or change a copy, if that works better. This way, your regex finds the position in a way consistent with what AWT is expecting.

Related

What's the meaning of the Emacs tab behaviored in IJ? and the behavior of the "cut line backward"?

The Emacs tab seems like three or four tabs(four spaces for each), I don't know the usage of it.
and the behavior of the "cut line backward" cuts the current space to the start with the above line?What's the logic?It shouldn't match the meaning "cut up to line end" and named "cut up to line beginning", they're together.So what is that functionality? I fully replace these two editings by selecting to start and end, but if you need deleting the selection,you as well type a space or else. So,the ummatching makes me unhappy.
Appreciate for resorting me!

Java: Display changing text on output

Currently, my program prints to System.out:
Processing line 1 of 3...
Processing line 2 of 3...
Processing line 3 of 3...
Is there any way to have it replace the line numbers, instead of printing a new line?
Well there are some hacks you can try, but none of them are guaranteed to work everywhere. Escape characters such as \b (backspace) can erase the last character and \r (replace) can erase the last output.
It may or may not work (depending on IDE), but you could try:
System.out.print("Hello");
Thread.sleep(1000);
System.out.print("\rWorld");
However, generally a Swing box should be used (dynamically updating GUI) to measure progress. See Progress Bars.

Divide string in lines

I am currently trying to fix my problem with string length at buttons, in android application. So i parse string and then set it as button text. But if one text is bigger then 9 characters or 10 it gets displayed out of the button overlay. I know i could make text smaller but this is not good solution because i already have small text.
So what you guys recommend for example if i have:
String text = "ThisIsSomeRealyLongText";
How can i split this string in 2 lines or when i reach character number 9 just use /n (to break text)?
Using too much text on a button is never a good idea. You should think about using an icon, or shorter text with (if needed) more explanation text somewhere else. Remember,the best UX will be the one with the least amount of reading involved.
Using regex you can do this. It will break the string into 9 characters per line.
text = text.replaceAll("(.{9})", "$1\n");
Use
(new line):
android:text = "ThisIsSome
RealyLongText"
But in code, you can just use \n. You can define in ../res/values/strings.xml:
<string name="longname">ThisIsSome\nRealyLongText</string>
then set the text for you button
android:text="#string/longname"

Write hyphen character in JTextpane until the line wraps

I've written some text values inside the JTextPane , below to that contents I want to write hyphen character, so the contents will be look like Headings..
Currently I'm inserting hyphen to the count of that content length.
If you need hyphenation in the JTextPane use the article showing how it could be implemented.
From the description it could be that you need usual bullets with hyphen sign. If yes there is another article shows how bullets could be achived.

JTextPane undo and redo whole words

I used the UndoManager to add undo and redo functionality to my JTextPane. However, it saves the text every time text is entered, so it undos to the last letter. How can I make it undo to the last word?
http://java-sl.com/tip_merge_undo_edits.html That's an example of such merging edits.
Use UndoableEdit#addEdit(). If you make non-whitespace edits stick together on the undo stack (by absorbing each other through this method) and whitespace edits too, then the next undo will act on the last work or last whitespace gap, which is what you want.

Categories