Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

DocumentListner Problem

I wan't to use a document listner to tell when 7 digits have been entered into a field...the code I have for this works fine.  However I want it to set the textfield back to empty after the 7 digits are entered, but using setText makes the command window spew.  How would I go about doing this?


Code:

scan_t1 = new JTextField(10);
scan_t1.getDocument().addDocumentListener(new MyDocumentListener());

class MyDocumentListener implements DocumentListener
{
public void insertUpdate(DocumentEvent e)
{
 updateLog(e, "inserted into");
}
public void removeUpdate(DocumentEvent e)
{   }
public void changedUpdate(DocumentEvent e)
{   }

public void updateLog(DocumentEvent e, String action)
{
String text = scan_t1.getText();
if (text.length() == 7)
{
    System.out.println(""+text+"\n");
    scan_t1.setText("");
}

}
         }
0
Element115
Asked:
Element115
  • 4
  • 3
  • 3
1 Solution
 
CEHJCommented:
Override insertString.

if(getLength() == 7) {
  remove(0, 7);
}
0
 
Element115Author Commented:
Got the following error.

unreported exception javax.swing.text.BadLocationException; must be caught or declared to be thrown doc.remove(0, 7);

I added:
   
PlainDocument doc;

doc = new PlainDocument();
textField = new JTextField();
textField.setDocument(doc);


if (doc.getLength() == 7)
{
doc.remove(0, 7);
}

0
 
CEHJCommented:
try
{
  if (doc.getLength() == 7)
  {
    doc.remove(0, 7);
  }
}
catch(BadLocationException e)
{
  e.printStackTrace();
}

This should appear in the overridden insertString of a subclass of plain document though.
0
Industry Leaders: 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!

 
Element115Author Commented:
Sorry I must seem dumb, but I don't know what that means.

With the try and catch I get: IllegalstateException: Attempt to mutate in notification.... in command window.
0
 
CEHJCommented:
>>catch I get: IllegalstateException: Attempt to mutate in notification

Ah yes, OK. I'll think of something.
0
 
TimYatesCommented:
Try:

try
{
 if (doc.getLength() == 7)
 {
  SwingUtilities.invokeLater(new Runnable()
                       {
                         public void run()
                         {
                           doc.remove(0, 7);
                         }
                       });  
 }
}
catch(BadLocationException e)
{
 e.printStackTrace();
}

0
 
TimYatesCommented:
Or, using your first code:

public void updateLog(DocumentEvent e, String action)
{
  String text = scan_t1.getText();
  if (text.length() == 7)
  {
    System.out.println(""+text+"\n");
    SwingUtilities.invokeLater(new Runnable()
                      {
                        public void run()
                        {
                          scan_t1.setText("");
                        }
                      });  
  }
}

Should work too :-)

I could be wrong though :-(

Hope not...

Good luck :-)
0
 
Element115Author Commented:
Thanx that worked like a charm.

CEHJ thanx for trying too.
0
 
CEHJCommented:
That's OK. That's not a good way of solving it, but if you're happy then that's OK.
0
 
TimYatesCommented:
CheesySolutions-R-Us ;-)
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!

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now