Highlight different text in jtextarea - java

These are the following things i want to know:
I want to highlight different text in jtextarea by selecting them by mouse.
I also want to get all the highlighted text later.
I also want to know how to remove particular highlighted line.
What code can i use ?

You need to use a Highlighter:
Highlighter hl = textArea.getHighlighter();
Read the API for the methods that allow you add, remove and display the highlights.

Related

How do I wrap text in a jTextPane?

I'm currently using a jTextArea, and that has a setLineWrap() method to indicate whether text should be wrapped or not. Do jTextPanes have an equivalent to this? I know it can wrap words, but I need it to wrap characters.
For example, "oooooooooooo..." should be wrapped and displayed as two or more lines of o's instead of one long line with a scroll bar. So is this possible with jTextPanes?
JTextPanes word wrap automatically, you have to set the size.
jTextPane.setSize(someValue);
The size should be set to something small.
But this doesn't help you because it doesn't do that with characters... Try using <br> maybe? Or check this link: http://java-sl.com/wrap.html

Dynamically highlighting strings in javaFX

Please read before labelling this as a duplicate.
I am creating an application that calculates how fast a person can type by calculating WPM and CPM. However I have hit a snag with the UI as I found out that you cannot really highlight individual strings in a TextArea. My goal is to compare what the user is typing to the random text that is generated by having it so that the text is being dynamically coloured or highlighted as the user is typing.
See http://10fastfingers.com/typing-test/english to get an idea of what I mean
I recently read the following post Highlighting Strings in JavaFX TextArea
I was trying to achieve the same goal of highlighting individual strings inside a javaFX TextArea until I realised that it pretty much is not possible. So I looked into TextFlow which does allow me to edit individual strings. The problem with TextFlow is that all 200 of the generated words would have to appear at once which is not what I want. With a TextArea not all the text has to be displayed at once.
This is what I have so far just so that you can further get an idea of where I am heading with this.

Changing the font of a text in a JTextPane dynamically

I want to change the certain texts written on a JTextPane dynamically. I have a String array containing the words which should be changed
String ListMethod [] = {"forward", "backward", "left", "right"};
I've gone through some posts and many suggest to use JTextPane or JEditorPane to edit text but most of the answers given work on static text. I want to do it in a way such that as I type "forward" or "backward", etc... in the textpane, it detects this words and changes the color. How can I go about it?
Thanks for your help.
See here how to implement a DocumentListener Value Change Listener to JTextField. The have a look at javax.swing.text.Highlighter and javax.swing.text.HighlightPainter.
You have to capture the appropriate event and perform actions. For example in your case you can create an ActionListener that changes color and use registerKeyBoardAction to attach it on your JTextPane.
Oracle has a good tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/index.html on event listeners. I suggest you start getting yourself familiar there

is that possible to built a simple text editor in java without using JTextArea?

I have questions according to my final project in my IT Faculty.. I have to make A simple text editor (like notepad) without using JTextArea (GUI Java), I have to make my own JTextArea..
Idea/Topic= Own Text Editor
Algorithms= doesnt have any idea yet
Data Structure= Rope(Member of Binary Trees)
Requirement= Typing without JTextArea...
Is that possible to make that? Can a panel (or something else like that) has a listener or make a listener to do that?
simply thinking, First step I just want to try for typing on a panel, absolutely algorithms needed but i dont get right algorithms so far.. Is that possible?
My thinking is, for text that im typing is stored to a tree.. Is that Rope(Member of Binary Trees) for my data Structure?
Thanks for helping...
It's possible.
Use as you mentioned JPanel attaching KeyListener and MouseListener.
You need a model (Document). For simplest case it could be e.g StringBuilder where you can add content and show the StringBuilder content.
You need caret position (int field) to indicate where the edit should happen.
You need Font field to keep all the font info to be used in the editor.
Override paintComponent() and use FontMetrics to measure the text and calculate necessary width/height. For the start I would create a text area without line wrap.
Implement viewToModel/modelToView methods. They should calculate position of caret for given x,y and x,y for given caret position.

Making words different colors in JTextField/JTextPane/?

I'm trying and failing to understand how to use Java's text editor components to colorize text as you insert it. I don't want or need a fully featured syntax highlighting library.
Basically, I have a JTextField (or some other JText... component), and a list of words. I want any words in the field that appear in the list to be red, and the rest of the words be green. So for example, if "fire" is in the list, "fir" would appear green and "fire" would appear red.
I've tried using a JTextPane and a DefaultStyledDocument, using a KeyListener to go over the text in the document and using AbstractStyledDocument.replace to replace the existing words with versions that have the correct attributes. This didn't do anything. What am I doing wrong?
Neither JTextPane nor JTextField isn't able to present formatted text, i.e text having more than one format. For text-editor-like capabilities like you'd find in WordPad or HTML, the component to use is the JEditorPane or its descendant, JTextPane.
The simplest thing you can do is set the ContentType of the JEditorPane to "text/html" and simply set its text to a string containing HTML. The Java structured text components are surprisingly competent with HTML; you can display tables and/or DIVs, and there is support for much of CSS2. Simplest to do your styles inline, but you can even do external style hrefs.
If you want to get fancy programmatically, you can access the DocumentModel and create text from spans of text each having their own formatting. The DocumentModel works essentially like a programmable text editor.
EDIT: Re-reading your question, I see my answer doesn't quite address it. Since you want multi-colored text JEditorPane is your only option; but rather than just piping in pre-colored text via HTML or such, you'll have to put a listener on your document model to catch changes introduced when you type; and after every document change you'll want to examine the text (again from the Document model) for text that should or should not be highlighted, and you'll want to apply formatting to certain runs of text.
There are devils in the details, but this should get you started.

Categories