I have a JTextArea in my Java program... I want that if I write an asterix (*) in the JTextArea, the output will be ATX as a text... can someone help me please?
you can do this
if(yourTextArea.getText.contains("*"))
{
yourTextArea.setText("ATX");
}
You can add a key listener to it, so when ever a key is typed, you check to see if it is an * and if it is replace it with the ATX
Oops, misread the question.
If you want to do this constantly, then the only way is through a keyListener:
http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html
In my opinion, this would probably be pretty annoying functionality for it to change one character to 3 characters as the user types. Consider doing it when the user does some method, in which case you wouldn't need a listener.
Do you mean:
String s = YourTextArea.getText().contains("*") ? "ATX" : YourTextArea.getText();
Please clarify your question so that we can help you
It sounds like you want to capture the user's input and then determine if you want to do something with it or not depending on what it is.
First off, you will need to add an action listener to this JTextArea to listen for whatever the user types so you can determine what it is. You would do this with a KeyListener. Next you need a way of determining where the caret is positioned in the JTextArea. You would do this with a CaretListener.
Now when ever a KeyPressed event occurs, you will need to determine what the key is. If it is in fact an asterisk * you will insert the text ATX into the JTextArea at the current position of the caret.
You will then use something like:
textarea.insert("ATX", pos);
Where textarea is the JTextArea object and pos is an integer that holds the current position of the caret within the JTextArea. If you are not sure how to get the caret position read up on the CaretListener API. It has everything you will need.
Output where? Into this text area itself? So, in other words do you want to implement some kind of alias?
In this case you can add KeyListener and in its keyTyped() implement your logic: if key is * add to text area your text.
Related
I'm trying to clear the JTextArea.
Currently, I'm using
jtextarea.setText(null);
What is the difference if I use
jtextarea.setText("");
There is no difference. They both have the effect of deleting the old text. From the java TextComponent page:
setText
public void setText(String t)
Sets the text of this TextComponent to the specified text. If the text is null
or empty, has the effect of simply deleting the old text. When text has been
inserted, the resulting caret location is determined by the implementation of
the caret class.
Note that text is not a bound property, so no PropertyChangeEvent is fired when
it changes. To listen for changes to the text, use DocumentListener.
Parameters:
t - the new text to be set
See Also:
getText(int, int), DefaultCaret
What the author was trying to was clear the JTextArea, not add a null character to it!
JTextArea0.selectAll();
JTextArea0.replaceSelection("");
This selects the entire textArea and then replaces it will a null string, effectively clearing the JTextArea.
Not sure what the misunderstanding was here, but I had the same question and this answer solved it for me.
JTextArea0.selectAll();
JTextArea0.replaceSelection("");
Actually There is the difference , i think so.
If you set it to null, The actual value written in text area will be nothing. But if you set it to "" it wil be an empty character. The same like you can set it to "z" and there will be written z, but null means unknow. You will not feal the difference until you gonna need to use the text written in textArea.
I'm writing a custom control, based on JTextField. My JTextField uses my own Document class, derived from PlainDocument, so that I can process all the user input in overriden insertString(...) and remove(...) methods.
Here's the problem. After I process user input, sometimes I want to move the caret to another position. What is the better way to do it?
By default Document puts the caret next to last input. So I tried to put a char to my target position and delete it immediately. For some reason it doesn't work in remove() method... and the code doesn't look nice :)
Thanks for and proposals.
It seems unnecessary to extend PlainDocument. Simply add a DocumentListener to the Document of your JTextField and you can process the user input in the 3 methods declared in DocumentListener
Use setCaretPosition to move the caret to wherever you would like
You should actually be using a DocumentFilter if you want to control user input. A DocumentFilter allows you to intercept all the input as it happens. You can then use JTextField.setCaretPosition (comes from JTextComponent) to set the caret position. Just pass your DocumentFilter implementation a reference to the JTextField so it can set the caret position for you.
Here's the Java trail for DocumentFilter. Also, an example on JavaRanch.
In JavaFX, in some text control like TextArea or similar, I want to be able to convert from mouse event coordinates to know exactly which character location is under the mouse. Is there some straightforward way to do that?
In Swing, you can use JTextArea.viewToModel, but I can't find the equivalent in JavaFX yet.
Specifically, I'm trying to find the location in the text (row/column) where mouse events occur. Knowing which side of the char it occurs on would be nice, too, though I don't know if that's supported.
Put another way, I want to decorate the character on mouse over.
I found the answer. It's:
TextArea text = ...
HitInfo hit = ((TextAreaSkin) text.getSkin()).getIndex(me);
Yes, it's in a com.sun.** package but I don't have a choice about that. I can hope that Oracle will someday make the equivalent public.
isLeading on HitInfo doesn't seem to work: it always returns true. And the insertion/char index are always the same even when they shouldn't be... But, at least I can get the insertion/char index. I suppose I should report bugs for those things but since they're not public APIs they probably won't care.
Perhaps coordinates of the mouse events are not necessary. You know that certain mouse events will move the carat (or selection). When you hear one of them inside your textarea, just ask the textarea where the carat wound up.
If that's acceptable, then getAnchor() and getCaretPosition() seem to be what you want.
From the javadoc for getCaretPosition()
The current position of the caret within the text. The anchor and
caretPosition make up the selection range.
Is there a way to validate text in a JTextField while you type, based on what you already typed in that field? Should I create a keyEventListener of some sort, or is there a way to override the insertString method to let it do that. I prefer the latter, but it only gives you control over the last character that was typed, not the entire string of text already present in the text field. I want it to beep as soon as one tries to enter more than one decimal point, and not adding that second decimal point to the text field.
Thanks for your help,
Erik
What's the problem with adding a listener? That's what they are there for.
You might also want to have a look at JFormattedTextField.
I am creating a SQL editor. I am using JTextPane for the editor. I want to implement AutoCompletion for table name etc. like Eclipse.
I think the appropriate class for displaying info on top of another component is JPopupMenu, which already handles layering correctly to display itself. JPopupMenu has a show() method that takes its 'parent' component as an argument, and it will show itself in that component's coordinate space. Since you want to display a selection of terms for the user to choose from, a menu seems appropriate.
To check for text changes, you'd add a DocumentListener to the document that's wrapped by the JTextPane; you can access it using getDocument().
To find out where the cursor (actually, the caret) is, you can use getCaretPosition(). That returns the caret's position within the text stream as an int. You can use modelToView() to translate that position to actual (x,y) coordinates. That in turn will tell you where to show your menu.
You can use addKeyListener() to catch keyboard events on your JTextPane, like hitting Ctrl-Space.
The combination of all that should allow you to do what you're looking to do.
You can also use http://fifesoft.com/autocomplete/. You can install it on any JTextComponent.
For things like this you probably should consider layered panes so your auto-complete suggestions appear in the correct place and z-order.
Furthermore you will have to look for changes in the JTextPane to know when the user is typing and you will need a parser that understands what is typed so you can offer the feature only at appropriate points.
It's not quite clear what exactly your problem is and what you got so far.
I achieved this by adding a key listener to the JTextPane and checking for CTRL + Space keystrokes. When the appropriate key combo was detected the listener went off and looked up the list of possible matches based on the characters directly to the left of the cursor at the time of the key press and found the best matches and displayed them to the user in a JPopup. If there was an exact match then it simply replaced the partial text with the match. If no matches were found an option was given to the user to add the text that they had already typed, edit it and record it into the list of acceptable data.
We use jide. They have a lot of components that help you do this kind of thing really easily