Solved

Clear jTextfield when using DocumentListener

Posted on 2011-03-25
6
1,047 Views
Last Modified: 2013-11-24
Hi

I am using a barcode scanner to enter text into a jTextfield, then using a DocumentListener (insertUpdate method) to detect inserted text in this field. When this is triggered it selectes the current item in a jComboBox, based on the text data in the jTextField, which in turn adds the current selected item onto a table.

The above functionality works, but the issue I have is that, although the text field regains focus, I cannot get the text field to be cleared of text, so that when another barcode is read it is not appended to the end of the current data.

Can you advise, with a code snippet, how I can clear the textfield ot its data after an event is registered and the action taken in the combobox and table. I have tried adding a foo.setSelectedText("") on the text field but this stops the above functionality from working.

Should I use an additonal listener on the text field? But how would I stop this from triggering before the DocumtnListener has actioned based on the current text in the field.

Code for the DocumentListener...

        main.tillPanel.txtBarcode.getDocument().addDocumentListener(new DocumentListener() {

            public void removeUpdate(DocumentEvent e) {
            }
            public void changedUpdate(DocumentEvent e) {
            }

            public void insertUpdate(DocumentEvent e) {

                String selectedBarcodeItem = main.tillPanel.txtBarcode.getText();
                for (int d = 0; d < roundStockObj.length; d++) {
                    if (roundStockObj[d].getBarcode().equals(selectedBarcodeItem)) {
                           
                   main.tillPanel.cmbTillProdSelect.setSelectedItem(roundStockObj[d].getDescription());
                        break;
                    }
                }
         }
});

Thanks in advance
0
Comment
Question by:BinaryFlusher
  • 4
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35218128
you just say jTexctFiled.setText("")

why set selected text?

I was always using just ActionListener in such cases
and it worked without problems.
You need to have the scanner configured that it should send carriage return after waht it is reading
Then of course I set
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35218143
you just say jTexctFiled.setText("")

why set selected text?

I was always using just ActionListener in such cases
and it worked without problems.
You need to have the scanner configured that it should send carriage return after waht it is reading
Then of course after processing in actionPerformed(ActionEvent e) method
I just say
jTextFiled.setText("");
and it clears it

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35218154
ActionListener will trigger only after it receives carriage return
(this is different from KeyListener, which will trigger all the time and you don't want it in this case)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:BinaryFlusher
ID: 35218318
for_yan

Many thanks, have done as you explained and works a treat.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35219735
Why were you using a DocumentListener in the first place?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35219784
You are always welcome.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now