Solved

Set Text to JText component on Applet

Posted on 2006-07-14
17
296 Views
Last Modified: 2008-02-07
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.IllegalStateException: Attempt to mutate in notification
      at javax.swing.text.AbstractDocument.writeLock(Unknown Source)
      at javax.swing.text.AbstractDocument.remove(Unknown Source)
      at org.tds.maseco.invoice.OutInvoice$DocListenerHandler.insertUpdate(OutInvoice.java:427)
      at javax.swing.text.AbstractDocument.fireInsertUpdate(Unknown Source)
      at javax.swing.text.AbstractDocument.handleInsertString(Unknown Source)
      at javax.swing.text.AbstractDocument.insertString(Unknown Source)
      at javax.swing.text.PlainDocument.insertString(Unknown Source)
      at org.tds.maseco.invoice.OutInvoice$LengthLimitedDocument.insertString(OutInvoice.java:449)
      at javax.swing.text.AbstractDocument.replace(Unknown Source)
      at javax.swing.text.JTextComponent.replaceSelection(Unknown Source)
      at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(Unknown Source)
      at javax.swing.SwingUtilities.notifyAction(Unknown Source)
0
Comment
Question by:CongUan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17106485
Hi CongUan

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
0
 
LVL 92

Expert Comment

by:objects
ID: 17106489
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 17106495
you should never update a document from a DocumentListener, instead try using a DocumentFilter.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 35

Expert Comment

by:girionis
ID: 17106498
Try to do the update from a different thread I'd say.
0
 

Author Comment

by:CongUan
ID: 17106529
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().addDocumentListener(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(DocumentEvent arg0) {
            // TODO Auto-generated method stub
        }

        public void insertUpdate(DocumentEvent docEvent) {
            int lengthOfText = docEvent.getDocument().getLength();
            if (OutInvoice.BARCODE_LENGTH_LIMIT == lengthOfText) {
                try {
                    String inputText = docEvent.getDocument().getText(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);
            }
        }

    }
0
 
LVL 92

Expert Comment

by:objects
ID: 17106544
> 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
0
 
LVL 35

Expert Comment

by:girionis
ID: 17106554
0
 
LVL 35

Expert Comment

by:girionis
ID: 17106559
Try

EventQueue.invokeLater(new Runnable()
{public void run() {
<your object>.setText("");
}
});
0
 
LVL 92

Expert Comment

by:objects
ID: 17106579
> EventQueue.invokeLater(new Runnable()

I'd be careful doing that in a doc listener
0
 
LVL 35

Expert Comment

by:girionis
ID: 17106595
> > EventQueue.invokeLater(new Runnable()

> I'd be careful doing that in a doc listener

Any thoughts as to why?
0
 
LVL 35

Expert Comment

by:girionis
ID: 17106597
> why?

I meant why not?
0
 
LVL 92

Expert Comment

by:objects
ID: 17106617
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

0
 

Author Comment

by:CongUan
ID: 17106625
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().addDocumentListener(new DocListenerHandler());


    protected void insertOutInvoice(String seriNumber) {
       /* SeriesNumber aSeriNumber = new SeriesNumber();
        aSeriNumber.setSeqNumber(outInvoiceList.size() + 1);
        aSeriNumber.setSeriCode(seriNumber);
        aSeriNumber.setProductName("KC36");
        outInvoiceList.add(aSeriNumber);
        tblObject.setOutInvoiceList(outInvoiceList);
        tblObject.refresh();
        pnTable.updateUI();
       */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                txtSeri.setText("");
            }
        });
    }

    class DocListenerHandler implements DocumentListener {

        public void changedUpdate(DocumentEvent arg0) {
            // TODO Auto-generated method stub
        }

        public void insertUpdate(DocumentEvent docEvent) {
            int lengthOfText = docEvent.getDocument().getLength();
            if (OutInvoice.BARCODE_LENGTH_LIMIT == lengthOfText) {
                try {
                    String inputText = docEvent.getDocument().getText(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);
            }
        }

    }
0
 
LVL 35

Expert Comment

by:girionis
ID: 17106635
> 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.
0
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 17106641
CongUan

you need to put the code in the document listener.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17106671
>>insertOutInvoice(inputText);

What does 'insertOutInvoice' actually do?
0
 
LVL 92

Expert Comment

by:objects
ID: 17106699
using a filter your method would look something like:

public void insertString(DocumentFilter.FilterBypass 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();
    }
    }
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 77
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 69
java mysql insert application 14 68
Java Eclipse Loop 3 46
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question