Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Clear jTextfield when using DocumentListener

Posted on 2011-03-25
6
Medium Priority
?
1,083 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 2000 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
Independent Software Vendors: 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!

 

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

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!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.
Suggested Courses

824 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