Solved

Clear jTextfield when using DocumentListener

Posted on 2011-03-25
6
1,062 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
[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
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
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

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

617 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