Deselect selected text in JTextPane - java

I have an editor in which I want to check that selected text will not contain some words. If it contains those particular words, then I need to deselect the selection made by user. Is there any way to do that in JTextPane?

I am not sure, but try this method.
textPane.setCaretPosition(start);

int end = pane.getSelectionEnd();
pane.setSelectionStart(end);
pane.setSelectionEnd(end);
This will deselect the selected text and leave the caret at the end of whatever the user selected. It might pay to pop a JOptionPane telling the user why the selection disappeared..
JOptionPane.showMessageDialog(
null,
"Don't select swear words!",
"Net Nanny says..",
JOptionPane.ERROR_MESSAGE);

Text selection is temporaray and contiguous. You can't unselect some text in the middle of a larger string of selected text.
Maybe you are talking about highlighting. Check out the API for getHighlighter(). You can add/remove highlights and specify the start/end offsets of each highlight.

Related

How to get text from jtextfield and skip to jtable row based on text?

I have 1 column with 100 names, all from A to Z. I have to choose 1. I want to be able to insert a few letters into a jtextfield, press enter, and have the row be selected that matches the text. How do I do this?
An example would be, I type "Bro", press enter and the selection skips to the first name that starts with "Bro". I dont mean a filter, I want to skip to it.
I have 1 column
So why are you using a JTable? Why not use a JList?
I want to be able to insert a few letters into a jtextfield, press enter,
You can do the search as every letter is typed (or removed), instead of forcing the user to press Enter.
Create a JTextField for entering the name to search for:
Add a DocumentListener to the Document of the text field. Read the section from the Swing tutorial on Listening For Changes on a Document for the basics.
In the DocumentListener you would get the model of your JList (or JTable) and then iterate through each item in the model to find the index of the first item that starts with the text in the text field.
If you use a JList then you just use the setSelectedIndex(...) methodto select the row and the ensureIndexIsVisible(...) method to scroll to the row
if you use a JTable then you use the changeSelection(...) method to select the row and then the scrollRectToVisible(...) method to scroll to the row using data from the getCellRect(...) method.

How to make the textfield remember history of words typed in GXT

Do we have an option to make the GXT textfield remember the history of the words that have been given as an input. So when i click the textfield next time, it should list all the searches below the textbox. Thanks in advance!
You can use an editable combobox. In order to look like text field and hide the arrow image in combobox, you can use setHideTrigger(false). So, you can have a combo box that functions like a text box. Also, you can add all the search terms in a store. That is it. You are done.

how to change color of the selected words in EditText

I'm creating a text editor, and i wont to able users to select some words in EditText and change color of the selected words,
How can I do that? Or how can i understand which words, user selected? Thanks
Use getSelectionStart and getSelectionEnd to get the active selection.
Use SpannableString and SpannableString.setSpan to colorize the selection using a ForegroundColorSpan.

Select text in JTextPane

I am creating a text editor using JTextPane that allows the user to bold, color and align the text by highlighting the text then selecting the option to either bold, color or align. After the adjustment is made, the selection goes away, but I want it to stay. Is there a way to get it to stay, or should I manually reselect the text? If so, how can I do that?
The example in Oracle's Java Tutorials does what you need and provides the source code.

how to select a line in a Jtextarea?

I have a jtextarea that is not editable. It has some text in it. What i want is that when a user clicks in the jtextarea, (preferably single click), the entire line be highlighted, and this highlighted text be retrieved.
Each line actually has an email of the form name#emailid.com. To select the entire text would require triple clicks. I want the email to be selected in a single click. Is this possible?
Sure. Just implement you own listener and call http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#select%28int,%20int%29 on the JTextArea.
But wouldn't a JList not rather fit your requirements?

Categories

Resources