0

There are 2 JTextField components and 1 JComboBox in my project.

When I input data to text fields, the combo box is adding separate items/row for each letter or number.

How can I fix that?

See the picture:

combobox picture

Here's my code:

t1.getDocument().addDocumentListener(new DocumentListener() {
          public void changedUpdate(DocumentEvent e) {
           changed();
          }
          public void removeUpdate(DocumentEvent e) {
           changed();
          }
          public void insertUpdate(DocumentEvent e) {
            changed();
        }

        public void changed() {
            if (!t1.getText().trim().isEmpty())
            {
                c1.addItem(t1.getText());
                }
          }
        });
    [Combobox adding separate row][1]
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Suavi
  • 9
  • 2
  • Because on each event you are calling change, instead you can do that when user hits enter or you can add a button, once its completely entered you can trigger the event. – Shivaraj Dec 14 '18 at 21:07
  • Hi @Suavi, can you describe a little bit more your question? For experienced developers it is possible to understand, but is good have in mind that your doubt could be the same of new developers too. – Jonatas Emidio Dec 14 '18 at 21:34
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Dec 15 '18 at 02:29

2 Answers2

1

According to what i understand from your problem,you want to add items into your combobox, once the user completes entering the full item name. For this:

Remove your document listener and instead use actionListener which is triggered automatically when a user presses enter.

Your code would be like:

t1.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
         if (!t1.getText().trim().isEmpty())
             c1.addItem(t1.getText());
    }
});
Dhyey Shah
  • 582
  • 7
  • 23
  • *"I want the user experience this answer offers!"* (With apologies to 'When Harry Met Sally'.) This is an excellent approach to the problem. – Andrew Thompson Dec 15 '18 at 04:32
0

Each time that your "t1" is changed you will add another item on your combo.

Instead of adding a listener in your textfield, you can add a FocusListener in your combo. There you will be able to get the textfield content and add in your menu in the open process.

You can do something like (maybe it is not the best option but will work):

        c1.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {}

        @Override
        public void focusGained(FocusEvent e) {
            c1.addItem(t1.getText);
        }
    });