Solved

Clear jTextfield when using DocumentListener

Posted on 2011-03-25
6
1,045 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:BinaryFlusher
Comment Utility
for_yan

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

0
 
LVL 86

Expert Comment

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

Expert Comment

by:for_yan
Comment Utility
You are always welcome.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 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

5 Experts available now in Live!

Get 1:1 Help Now