How to clear JTextArea? - java

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.

Related

convert a method to work with gui

I've got a huge method that prints multiple lines, numbers, characters and uses system.out and multiple data types, it works. But I'd like to use it in a jframe. I tried converting every system.out statement to a jtextArea.setText(), and did casting for non string types but nothing comes out when I run it.
Is it possible? what is the right way of doing that.
jtextarea right for my method.
If you wish to append text to a JTextArea, use the append method. Right now your code is using setText, which does just that: sets the text of the JTextArea, removing all previous text in the process (and in this way your code seems to almost guarantee that the JTextArea either contain no text, or contain a single new line character).
try jTextField if its only gonna output.
Try jTextField.append if the user will write stuff on the text.

Clarification on modifying a Document's contents from a DocumentListener

From the Swing tutorial on text components:
You may want to change the document's text within a document listener. However, you should never modify the contents of a text component from within a document listener. If you do, the program will likely deadlock. Instead, you can use a formatted text field or provide a document filter.
I am confused. So what is the correct way to change the text eg. as a result of a KeyEvent ?
1) using DocumentListener for
output from JTextComponent to the GUI
HightLighter or Styled text
2) DocumentFilter for filtering of
unwanted chars,
chars sequence(s),
these filtered chars could be
replaced with another char (or with defined chars sequence)
removed (never will be displayed in the JTextComponent)
3) similair funcionality to provide JFormattedTextFieldis possible to input to the JTextComponent only chars 0 - 9, decimal separator, negative sing,
4)
So what is the correct way to change the text eg. as a result of a KeyEvent ?
use DocumentFilter
A direct answer is using SwingUtilities.invokeLater() placing the Document modification code there. But mKorbel's answer (+1) covers most of the cases you can imagine.
The text say's that you may want to use a document listener. Here is a example how to write one.
A Swing text component uses a Document to represent its content.
Document events occur when the content of a document changes in any
way.
So, always that your text component changes the document listener will fire, but the text says that you cannot change the value of the component in this listener.
In a KeyListener (that's not a document listener) you can change the value using setText().
Depending on what you want, i suggest you look DocumentFilter.

Reading text from JTextArea in java

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.

Java StyledDocument: How can I ensure that what the user types never appears already styled?

The user types some text. When they press a button, what they have typed is split up and colour coded:
colors.setCharacterAttributes(characters, tokens[x].length(), formatBlue, true);
Using a set of rules.
When they make an edit between the position as defined by characters and the position characters + tokens[x].length() it comes up in my formatBlue style.
However, I would like it to be in black until the user next presses the 'colour code' button I have.
In short: the desired effect is that everything that is typed should always in black, until it has been phrased and coloured differently by the program.
So far, the best solution I have is to detect when the caret changes position, and then do:
setLogicalStyle(textArea.getCaretPosition(), formatBlack)
Any better suggestions would be appreciated.
the desired effect is that everything that is typed should always in black
You could try using a DocumentFilter. If the text about to be inserted does not contain an attribute then you assign the default black attribute.
Read the section from the Swing tutorial on Implementing a Document Filter for more information.
You can get EditorKit from your JEditorPane. It's StyledEditorKit instance. So you can get InputAtributes from the kit and remove all the attributes. Thus all the typing will use the empty AttributeSet.

Input-validation based on entire content of JTextField, not just the last typed character

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.

Categories

Resources