CongUan
asked on
Set Text to JText component on Applet
Hi Expert,
Please help me with set a string to jText on a applet.
on Init I can set a text to a txtSeri text box, but when I get input from user and set text again, it cause me the problem as below.
Can you help me for this? urgent!
java.lang.IllegalStateExce ption: Attempt to mutate in notification
at javax.swing.text.AbstractD ocument.wr iteLock(Un known Source)
at javax.swing.text.AbstractD ocument.re move(Unkno wn Source)
at org.tds.maseco.invoice.Out Invoice$Do cListenerH andler.ins ertUpdate( OutInvoice .java:427)
at javax.swing.text.AbstractD ocument.fi reInsertUp date(Unkno wn Source)
at javax.swing.text.AbstractD ocument.ha ndleInsert String(Unk nown Source)
at javax.swing.text.AbstractD ocument.in sertString (Unknown Source)
at javax.swing.text.PlainDocu ment.inser tString(Un known Source)
at org.tds.maseco.invoice.Out Invoice$Le ngthLimite dDocument. insertStri ng(OutInvo ice.java:4 49)
at javax.swing.text.AbstractD ocument.re place(Unkn own Source)
at javax.swing.text.JTextComp onent.repl aceSelecti on(Unknown Source)
at javax.swing.text.DefaultEd itorKit$De faultKeyTy pedAction. actionPerf ormed(Unkn own Source)
at javax.swing.SwingUtilities .notifyAct ion(Unknow n Source)
Please help me with set a string to jText on a applet.
on Init I can set a text to a txtSeri text box, but when I get input from user and set text again, it cause me the problem as below.
Can you help me for this? urgent!
java.lang.IllegalStateExce
at javax.swing.text.AbstractD
at javax.swing.text.AbstractD
at org.tds.maseco.invoice.Out
at javax.swing.text.AbstractD
at javax.swing.text.AbstractD
at javax.swing.text.AbstractD
at javax.swing.text.PlainDocu
at org.tds.maseco.invoice.Out
at javax.swing.text.AbstractD
at javax.swing.text.JTextComp
at javax.swing.text.DefaultEd
at javax.swing.SwingUtilities
you're trying to update the document in one of your listeners (DocListenerHandler), but as it is in the process of being editted you can't.
you should never update a document from a DocumentListener, instead try using a DocumentFilter.
Try to do the update from a different thread I'd say.
ASKER
Yah, I think I understand these,
But how to set text with different thread, Please give me some suggestion with my code as below:
txtSeri.setDocument(new LengthLimitedDocument());
txtSeri.getDocument().addD ocumentLis tener(new DocListenerHandler());
I implement LengthLimitedDocument() because I want to limit the lengt of text of input, Do you have any way to limit the lengt of input of jText?
After insertOutInvoice(inputText ) called, I want to set text txtSeri to "" (empty).
Can you have me agian?
class DocListenerHandler implements DocumentListener {
public void changedUpdate(DocumentEven t arg0) {
// TODO Auto-generated method stub
}
public void insertUpdate(DocumentEvent docEvent) {
int lengthOfText = docEvent.getDocument().get Length();
if (OutInvoice.BARCODE_LENGTH _LIMIT == lengthOfText) {
try {
String inputText = docEvent.getDocument().get Text(0,
lengthOfText);
// printBarcode(inputText);
insertOutInvoice(inputText );
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void removeUpdate(DocumentEvent arg0) {
// TODO Auto-generated method stub
}
}
class LengthLimitedDocument extends PlainDocument {
public void insertString(int offset, String str, AttributeSet attr) throws
BadLocationException {
if (str != null &&
(getLength() + str.length()) <= OutInvoice.BARCODE_LENGTH_ LIMIT) {
super.insertString(offset, str, attr);
}
}
}
But how to set text with different thread, Please give me some suggestion with my code as below:
txtSeri.setDocument(new LengthLimitedDocument());
txtSeri.getDocument().addD
I implement LengthLimitedDocument() because I want to limit the lengt of text of input, Do you have any way to limit the lengt of input of jText?
After insertOutInvoice(inputText
Can you have me agian?
class DocListenerHandler implements DocumentListener {
public void changedUpdate(DocumentEven
// TODO Auto-generated method stub
}
public void insertUpdate(DocumentEvent
int lengthOfText = docEvent.getDocument().get
if (OutInvoice.BARCODE_LENGTH
try {
String inputText = docEvent.getDocument().get
lengthOfText);
// printBarcode(inputText);
insertOutInvoice(inputText
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void removeUpdate(DocumentEvent
// TODO Auto-generated method stub
}
}
class LengthLimitedDocument extends PlainDocument {
public void insertString(int offset, String str, AttributeSet attr) throws
BadLocationException {
if (str != null &&
(getLength() + str.length()) <= OutInvoice.BARCODE_LENGTH_
super.insertString(offset,
}
}
}
> I implement LengthLimitedDocument() because I want to limit the lengt of text of input, Do you have any way to limit the lengt of input of jText?
thats fine, its your listener thats the problem
> After insertOutInvoice(inputText ) called, I want to set text txtSeri to "" (empty).
you cannot do that in a DocumentListener
th code u posted does not appear to be the same as what caused the error
thats fine, its your listener thats the problem
> After insertOutInvoice(inputText
you cannot do that in a DocumentListener
th code u posted does not appear to be the same as what caused the error
Have a look here: http://forum.java.sun.com/thread.jspa?threadID=620748&tstart=75
Try
EventQueue.invokeLater(new Runnable()
{public void run() {
<your object>.setText("");
}
});
EventQueue.invokeLater(new
{public void run() {
<your object>.setText("");
}
});
> EventQueue.invokeLater(new Runnable()
I'd be careful doing that in a doc listener
I'd be careful doing that in a doc listener
> > EventQueue.invokeLater(new Runnable()
>
> I'd be careful doing that in a doc listener
Any thoughts as to why?
>
> I'd be careful doing that in a doc listener
Any thoughts as to why?
> why?
I meant why not?
I meant why not?
cause your triggering an update event from another update event.
you should instead use a DocumentFilter
http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filter
you should instead use a DocumentFilter
http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filter
ASKER
Hi, I try to do with
EventQueue.invokeLater(new Runnable()
{public void run() {
txtSeri.setText("");
}
});
but it cause the same problem.
Any help agian.
The below is all my code that cause the problem.
txtSeri.setDocument(new LengthLimitedDocument());
txtSeri.getDocument().addD ocumentLis tener(new DocListenerHandler());
protected void insertOutInvoice(String seriNumber) {
/* SeriesNumber aSeriNumber = new SeriesNumber();
aSeriNumber.setSeqNumber(o utInvoiceL ist.size() + 1);
aSeriNumber.setSeriCode(se riNumber);
aSeriNumber.setProductName ("KC36");
outInvoiceList.add(aSeriNu mber);
tblObject.setOutInvoiceLis t(outInvoi ceList);
tblObject.refresh();
pnTable.updateUI();
*/
EventQueue.invokeLater(new Runnable() {
public void run() {
txtSeri.setText("");
}
});
}
class DocListenerHandler implements DocumentListener {
public void changedUpdate(DocumentEven t arg0) {
// TODO Auto-generated method stub
}
public void insertUpdate(DocumentEvent docEvent) {
int lengthOfText = docEvent.getDocument().get Length();
if (OutInvoice.BARCODE_LENGTH _LIMIT == lengthOfText) {
try {
String inputText = docEvent.getDocument().get Text(0,
lengthOfText);
insertOutInvoice(inputText );
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void removeUpdate(DocumentEvent arg0) {
// TODO Auto-generated method stub
}
}
class LengthLimitedDocument extends PlainDocument {
public void insertString(int offset, String str, AttributeSet attr) throws
BadLocationException {
if (str != null &&
(getLength() + str.length()) <= OutInvoice.BARCODE_LENGTH_ LIMIT) {
super.insertString(offset, str, attr);
}
}
}
EventQueue.invokeLater(new
{public void run() {
txtSeri.setText("");
}
});
but it cause the same problem.
Any help agian.
The below is all my code that cause the problem.
txtSeri.setDocument(new LengthLimitedDocument());
txtSeri.getDocument().addD
protected void insertOutInvoice(String seriNumber) {
/* SeriesNumber aSeriNumber = new SeriesNumber();
aSeriNumber.setSeqNumber(o
aSeriNumber.setSeriCode(se
aSeriNumber.setProductName
outInvoiceList.add(aSeriNu
tblObject.setOutInvoiceLis
tblObject.refresh();
pnTable.updateUI();
*/
EventQueue.invokeLater(new
public void run() {
txtSeri.setText("");
}
});
}
class DocListenerHandler implements DocumentListener {
public void changedUpdate(DocumentEven
// TODO Auto-generated method stub
}
public void insertUpdate(DocumentEvent
int lengthOfText = docEvent.getDocument().get
if (OutInvoice.BARCODE_LENGTH
try {
String inputText = docEvent.getDocument().get
lengthOfText);
insertOutInvoice(inputText
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void removeUpdate(DocumentEvent
// TODO Auto-generated method stub
}
}
class LengthLimitedDocument extends PlainDocument {
public void insertString(int offset, String str, AttributeSet attr) throws
BadLocationException {
if (str != null &&
(getLength() + str.length()) <= OutInvoice.BARCODE_LENGTH_
super.insertString(offset,
}
}
}
> cause your triggering an update event from another update event.
Yes, but won't this update go to the end of the event queue? It will only happen when the first update has finished.
> you should instead use a DocumentFilter
TBH I've never used a DocumentFilter, I guess it's a solution as well.
Yes, but won't this update go to the end of the event queue? It will only happen when the first update has finished.
> you should instead use a DocumentFilter
TBH I've never used a DocumentFilter, I guess it's a solution as well.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>insertOutInvoice(inputTe xt);
What does 'insertOutInvoice' actually do?
What does 'insertOutInvoice' actually do?
using a filter your method would look something like:
public void insertString(DocumentFilte r.FilterBy pass fb, int offset, String string, AttributeSet attr) {
int lengthOfText = fb.getDocument().getLength ();
if (OutInvoice.BARCODE_LENGTH _LIMIT == lengthOfText) {
try {
fb.remove(0, lengthOfText);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void insertString(DocumentFilte
int lengthOfText = fb.getDocument().getLength
if (OutInvoice.BARCODE_LENGTH
try {
fb.remove(0, lengthOfText);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Are you updating the model when you change the value of the input? Can you post the code you do the update (you set the text)?
Cheers