Anybody know what the heck is IllegalArgumentException..
Hi,
Anybody know what exactly means by IllegalArgumentAcception? In my program the error message print out like this:
java.lang.IllegalArgumentException: bad position: 1at javax.swing.text.JTextComponent.setCaretPosition(JTextComponent.java:1650)
From my coding:
try
{
String val ="A";
int pos = getCaretPosition();
getDocument().insertString(getCaretPosition(),val,null);
setCaretPosition(pos+1);
moveCaretPosition(pos+1);
requestFocusInWindow();
}catch(Exception input)
{ input.printStackTrace();}
It seems that the setCaretPosition(pos+1)
in my coding facing problem. But I sure that the coding correct. Is there something out of my sight or something I incorrectly put in there. I need opinion on this matter. Can anybody help?
thanks
# 6
> Is it possible that the exception occured because the JTextField have nothing or there is no word or character in the JTextField.
You tell us. You wrote the code. You are looking at the GUI. Its a simpler error to fix. Use:
textComponent.getDocument().getLength();
This will tell you how many characters are in the Document. You can only position the caret between 0 and the value from above.
# 8
Still don't understand. I've try to increase the position by adding like this:
int pos = getDocument().getLength() + 1;
.
Now the exception message stated like this:
java.lang.IllegalArgumentException: bad position: 2
at javax.swing.text.JTextComponent.setCaretPosition(JTextComponent.java:1650)
Argggggh it give me headache...
# 9
If you have a text component with the following text: "a", then you can set the caret position at either 0, 1 (before the "a" and after the "a")
If you have a text component with the following text: "ab", then you can set the caret position at either 0, 1, 2 (before the "a" and after the "a" and after the "b")
I don't understand your confusion. You can only set the caret position before and existing character or after the last character.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.