Write hyphen character in JTextpane until the line wraps - java

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.

Related

Highlighting specific instances of a string for output to TextArea

I have a search function that scrolls through a String and finds specific instances of a substring and then notes the index for the location of the substring.
For example, if I'm trying to find the String
"AATACG"
in the string
"TACGATCAATACGACGATCAGT",
it will return 7 as the index of the substring. What I need is a way to color the substring. So, the return text will be
"TACGATCAATACGACGATCAGT",
but with the substring colored. The text is outputted to a JavaFX TextArea.
I've tried using ANSI codes (which didn't work); I've also tried changing the string into a Text object and setting the fill color/applying a CSS id, but I can't find a way to change the Text back into a String with the color change remaining.
Is there a way to do this? Any help is much appreciated.
How about using javafx.scene.text.TextFlow? Seems like you want to highlight text without editing it. In this case TextFlow is appropriate component.

how to create persian content in pdf using eclipse

I have a problem with inserting UNICODE characters in a PDF file in eclipse.
There is some solution for this that it is not very efficient for me.
The solution is like this.
document.add(new Paragraph("Unicode: \u0418", new Font(bfComic, 12)));
I want to retrieve data from a database and show them to the user and my characters are in Arabic script and sometimes in Farsi script.
What solution do you suggest?
thanks
You are experiencing different problems:
Encoding of the data:
Please download chapter 2 of my book and go to section 2.2.2 entitled "The Phrase object: a List of Chunks with leading". In this section, look for the title "Database encoding versus the default CharSet used by the JVM".
You will see that database values are retrieved like this:
String name1 = new String(rs.getBytes("given_name"), "UTF-8");
That’s because the database contains different names with special characters. You risk that these special characters are displayed as gibberish if you would retrieve the field like this:
String name2 = rs.getString("given_name")
Encoding of the font:
You create your font like this:
Font font = new Font(bfComic, 12);
You don't show how you create bfComic, but I assume that this object is a BaseFont object using IDENTITY_H as encoding.
Writing from right to left / making ligatures
Although your code will work to show a single character, it won't work to show a sentence correctly.
Suppose that name1 is the Arabic version of the name "Lawrence of Arabia" and that we want to write this name to a PDF. This is done three times in the following screen shot:
The first line is wrong, because the characters are in the wrong order. They are written from left to right whereas they should be written from right to left. This is what will happen when you do:
document.add(name1);
Even if the encoding is correct, you're rendering the text incorrectly.
The second line is also wrong. The characters are now in the correct order, but no ligatures are made: ل followed by و should be combined into a single glyph: لو
You can only achieve this by adding the content to a ColumnText or PdfPCell object, and by setting the run direction to PdfWriter.RUN_DIRECTION_RTL. For instance:
pdfCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
Now the text will be rendered correctly.
This is explained in chapter 11 of my book. You can find a full example here: Ligatures2

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"

JTextPane - dynamic word wrap

I tried to use jTextPane1.setText("xxx xxxx xxx xxxxx xx xxx xxxx xxx etc..."); but JTextPane does not word wrap it at all showing all the text in one line only instead.
It would be interesting to support word wrap on jTextPane1 resized too...
So my question is...
how to make JTextPane support word wrap?
Try using a JTextArea and call setWrapStyleWord(true); on its instance this should do what you need.
EDIT:
If you need to use a JTextPane as a requirement(which you said you do), then have a look at a similar question that I found which answer should be of help: How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?
I had the same problem, the solution from David Kroukamp was very helpful. I changed it to JTextArea and set the following properties, as described in this tutorial:
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
Why not use a JTextArea instead of a Pane?
http://docs.oracle.com/javase/1.5.0/docs/api/
public void setWrapStyleWord(boolean word)
Sets the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundaries. By default this property is false. Parameters: word - indicates if word boundaries should be used for line wrapping See Also: getWrapStyleWord()

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

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.

Categories