Highlighting specific instances of a string for output to TextArea - java

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.

Related

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"

How to set word wrap for JComboBox?

I want to display multiple-line text for each item in combobox. Words should go to the next line automatically if the exceed the width of combobox. How to achieve this?
String meanings;// array which contains pretty long sentences
JComboBox<String> meaningsComboBox=new JComboBox<String>(meanings);
Maybe wrapping each value into html will help you(this trick work with JLabels), example here.
In another way, I think, you need make custom renderrer, example here

Getting style from any offset in JTextPane

Is there a way to get Style, a style name or just even compare whether Style at a certain position of JTextPane with the style I gave the text when inserting? Because for my purpose I created custom JTextPane, StyledDocument and DocumentFilter. So I could choose Style to be used for say regular letters and another Style for numbers. I have also toggle button which while toggled sets DocumentFilter to format numbers differently and while not toggled numbers format regularly so at the end you can't distinguish which numbers have been affected just according to JTextPane's getText() method. Therefore the only way would be to compare styles which I have both regular and special number style as constants. Only thing I need to come up with is how to get Style for each character.
I know there is JTextPane's method to get AttributeSet from the caret's position called getCharacterAttributes() but I think it's no use for my issue.
Is it necessary to include code example? I don't think it's difficult to imagine. If you want me I will include it though.
Any input would be appreciated. Thanks!
Try calling StyledDocument.getCharacterElement(pos) to get the character element at that position and then call Element.getAttributes() to get its attribute set.
The AttributeSet contains styles which you can retrieve using methods provided by StyleConstants.

Scrolling TextView in a ScrollView to a specific substring of text

For starters, I'm a C++/Qt Developer jumping into Android/Java development, so please don't assume I know anything. :) So, I have an application with a TextView contained with in a ScrollView. The text contained in the TextView is the results of a web query from my web server application. Depending on the query, the text can be pretty long. I want to implement a search feature on the text where the user enters some text string to search for, and if the text string is found, the ScrollView will scroll the TextView to ensure the entered substring is visible, and I want the text highlighted. I think I know how to highlight the text substring, I know how to tell the ScrollView to scroll to a specific line number in the TextView, but for the life of me, I can't figure out how to find out what line number to scroll to to guarantee my substring is visible. Ie, I want to query the text view to give me the line number that the substring first occurs at. Eventually, I want to also implement a find next that goes to the next occurrence of the substring. Kind of like the Find feature in Firefox. Any assistance is greatly appreciated. Thanks in advance.
You need to find the position of the substring in the text yourself, e.g. with String.indexOf(). Once you know that position, you can look up the correct line in the TextView using the Layout.getLineForOffset() function:
int offset=myString.indexOf("search string");
int line = myTextView.getLayout().getLineForOffset(offset);

Selecting specified text in an HTML formatted JEditorPane

I am displaying text in a Java JEditorPane using HTML to fomrat the text. I am also designing a search function that finds text in the JEditorPane selects the text and then scrolls to it. My problem is creating an algorithim that will actually specify the beginning and ending position for the selection.
If I simply retrieve the text using myeditorpane.getText(), then find the search string in the result, the wrong selection start and end positions are calculated with the wrong text being selected (the tags are throwing the calculation off). I tried removing the html tags by executing a replace all function text.().replaceAll("\<.*?>","") before searching for the text (this replace all removes all text in between the tags) but still the wrong selection points are calculated (although I'm getting close :-)).
Does anyone have an easy way to do this?
Thanks,
Elliott
You probably want to be working with the underlying Document, rather than the raw text, as suggested in this HighlightExample.
You need to find the start location of the text. I guess something like:
int offset = editorPane().getDocument().getText().indexof(...);
Then to scroll you can use:
editorPane.scrollRectToVisible( editorPane.viewToModel(offset) );
Read up on Text and New Lines for more info.

Categories