I use a JTextArea in which I use setText method to have some text while opening GUI.
Once text area is opened with the text I set, I typed some text, my intention is to get whatever text user types.
dataField = new JTextArea();
dataField.setText("sample#");
..
...
If I type "hello world"
sample#hello world
in text area and press enter I need to get only hello world in a string and not sample#hello world. I have tried with key listeners and
appended the input characters to a string builder but backspace also creates a unreadable character an appends
to it.
Simply put, I need to get user typed text from text area.
sample# is the last line in text area , so what ever i type after the last token i need to get that text.
Read the JTextArea API:
getLineCount() so you know the number of lines
getLineStartOffset() and getLineEndOffset() to know the offsets of the text you need to get from text area. Add 7 to the start since you don't want to get the "sampler#" text.
getText() to get the text using the offsets from above.
String a="sample#"+datafield.getText();
Try this
String old = "sample#";
String s = dataField.getText().subString(old.length());
Good luck
Related
I am trying to highlight text in a JEditorPane text field and save the index of the selected text. However I keep running into problems when I save the selections. The JEditorPane seems to save new lines in a different way each time. It will write a "\n", a "\r" and sometimes even a "\r\n" when the user presses the enter key. And sometimes the JEditorPane will even ignore carriage returns which causes wrong selection indices.
Is it possible to get a consistent behavior when the user presses the enter key?
Why are you using a JEditorPane. That is for displaying HTML and HTML generally doesn't have line breaks. You use the "br" tag in HTML. For normal text you should be using either a JTextPane or JTextArea.
I am trying to highlight text
The text in a string depends on the component you are using and whether you get the text from the Document or the component.
In general you should get the text from the Document since the Document only stores the "\n" so you should be able to calculate the offset correctly and highlight the text.
When you get the text from the component, the platform new line String is inserted in the text which would cause your offset problems.
See Text and New Lines for more information.
I have a display text area in my JavaFX app.
It has several methods for appending strings to it.
What I want is for any newly appended text to have some kind of feedback so the user is more visually aware of the new text being displayed.
Ideally what I would like is one of the following, but am very open to other suggestions:
The appended text is 'typed out' as if someone were typing it into the display text area itself. Or..
Some kind of highlighting of the new text as it appears.
Although again, I'm open to suggestions!
Thank you.
As suggested in Highlighting Strings in JavaFX TextArea, you can select the new text.
Assuming the text area is not editable, and you are only ever appending to the end of the existing text, you can do:
textArea.textProperty().addListener((obs, oldText, newText) -> {
if (newText.length() > oldText.length()) {
Platform.runLater(() ->
textArea.selectRange(oldText.length(), newText.length()));
}
});
If your text area is editable, or you are using it more generally, then you can do something similar each time you append text. For example:
String message = ... ;
textArea.positionCaret(textArea.getLength());
textArea.appendText(message);
textArea.extendSelection(textArea.getLength());
Maybe try to use RichTextArea (https://github.com/TomasMikula/RichTextFX), then is many possibilites to show your text in different ways.
I'm making a list of news. Each element could be 1-3 lines height.
Also, there must be a date in the right, text should end before it, but at the 2nd line text should be under date text, and in the end, if text is too long, it should end with "...", but (!) last word (name of news company) must stay as it is. Header of news could be any lenght and it must be white, text of news also could be any length and must be gray. If the headline goes over more than one line, then the main text can begin right
after the headline in the same line.
What i already know:
For "..." at the end of the text i want to use android:ellipsize="end", for different coloring of text SpannableString and ForegroundColorSpan.
What i need: how to make text in one TextView that would have different length of each line and would cut text before last word. Maybe it's possible using multiple TextView's, i'm not sure, cause header could be on 1 line or on 2 lines.
Example of what i need at the end.
Header header header header DATE
header Text text text text text text
text text text text text text...Name
Thank you for any answers and advices.
Make you text as html text, break it using html tag and set that text to textview using Html.fromhtml(htmltext);
I am creating a text editor in java that saves the text as html, but displays as plain text to the user. The user has the ability to change the color, alignment and style (bold and underline) of the text. The entire body of text is stored in the database as html in order to save the style adjustments. I am having an issue with newlines not being saved. So when the user is entering text, presses enter and puts text on a new line, it all gets put on one line after be saved and re-displayed. All the text is just being put inside on paragraph tag without any line breaks. I'm wondering if there's a way to tell the text editor to automatically insert line breaks for new lines?
The way I have my editor set up is a JTextPane using a HTMLEditorKit with content type set to text/html. I am using StyledEditorKit actions for changing the color and style (bold, underline) of the text and StyleConstants.setAlignment for changing the text alignment (I had some issues with the StyledEditorKit.AlignmentAction). Let me know if you need any specific source code.
try encapsulating your text inside an html < p > tag - it should maintain line breaks.
I assume there is a key down event for this kit? Assuming you have a KeyEvent handler you should be able to do something like this:
if(e.getKeyCode() == KeyCodes.KEY_ENTER){
editor.Text += "<br />";
}
I have a JSP page with a textarea HTML component. When a user presses enter and goes to next line while typing in textarea,and when he clicks save button, it saves the data from the text area to the database. But when I load the content of the database, the line breaks are gone. Means:
HELLO
WORLD
is displaying like
HELLO WORLD
I tried the method given here How do I replace all line breaks in a string with <br /> tags?
but its not working. I also tried this one How to save user-entered line breaks from a TextArea to a database? but I am not using PHP and also the solution given here for another languages to replace "\n" with "<br />" also not working. Please anyone have any idea how to do that?
I tried this code:
String a=req.getParameter("topicdes").toString();
a.replaceAll("\n","<br />");
The replaceAll method return a new String. Your variable a is never modified.
To solve your problem : a = a.replaceAll("\n","<br />");
In this kind of method, reading the javadoc is time saver.
Regards.