?
Solved

DocumentListner Problem

Posted on 2003-03-13
10
Medium Priority
?
204 Views
Last Modified: 2010-03-31
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
Comment
Question by:Element115
[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
  • 4
  • 3
  • 3
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127144
Override insertString.

if(getLength() == 7) {
  remove(0, 7);
}
0
 
LVL 1

Author Comment

by:Element115
ID: 8127400
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127445
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:Element115
ID: 8127555
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8127588
>>catch I get: IllegalstateException: Attempt to mutate in notification

Ah yes, OK. I'll think of something.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8127922
Try:

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

0
 
LVL 35

Accepted Solution

by:
TimYates earned 120 total points
ID: 8127942
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
 
LVL 1

Author Comment

by:Element115
ID: 8128338
Thanx that worked like a charm.

CEHJ thanx for trying too.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8128466
That's OK. That's not a good way of solving it, but if you're happy then that's OK.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8128570
CheesySolutions-R-Us ;-)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 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