In Java / Swing, is there a way to legitimately "try to mutate in a notification"?

I was wondering if there is any magic that I can use to get around the IllegalStateException and let JTextField "try to mutate in the notification" or, in other words, set its own text if its listener starts.

For your information, I am trying to program an autocomplete function that returns the most likely match in a range of 12 enumerations in response to user input in JTextField.

Here is a sample code. You will have to have mercy on my clumsy algorithm that creaks the enumeration results. I highlighted the code that throws an exception with a comment:

jtfElement1.addCaretListener(new CaretListener() {
            @Override
            public void caretUpdate(CaretEvent e) {                    
                String s = jtfElement1.getText();
                int[] attributes = new int[13];
                // iterate through each enum
                for (BaseEnumAttributes b: BaseEnumAttributes.values()) {
                    // iterate through the length of the current text in jtfElement1
                    for (int i = 0; i < s.length(); i++) {
                        if (s.length() <= b.toString().length()) {                                
                            if (b.toString().charAt(i) == s.charAt(i)) {
                                // increase the number of "hits" noted for that enum
                                attributes[b.ordinal()] = attributes[b.ordinal()] + 1;
                            }                                
                        }
                    }                        
                }
                int priorC = 0;
                int rightC = 0;                    
                // iterate through the "array" of enums to find the highest score
                for (int j = 0; j < attributes.length; j++) {
                    if (attributes[j] > priorC) {
                        priorC = attributes[j];
                        rightC = j;
                    }
                }                    
                if (!s.equals("")) {
                    // assign to b the Enum corresponding to the "array" with highest score
                    BaseEnumAttributes b = BaseEnumAttributes.values()[rightC];
                    iController.updateInputElement1String(b.toString());                        
                    // THIS TRIGGERS EXCEPTION 
                    jtfElement1.setText(b.toString());
                }

            }
        });
+3
6

, .

, ?

+5

SwingUtilities.invokeLater(),

+5

, setText() Thread caretUpdate().

0

, :

caretUpdate() if (false), u'r jTextField, ., - :

boolean caret = true;

private void listValueChanged (javax.swing.event.ListSelectionEvent evt) {       caret = false;       name.setText((String) list.getSelectedValue());       caret = true;   }

private void nameCaretUpdate(javax.swing.event.CaretEvent evt) {
   if(caret){
    model = new DefaultListModel();
    this.fillList(name.getText());
    list.setModel(model);
    }
}
0
source

Create your own document and override insertString ()

filenameText = new JTextField(new FilenameDocument(), "", 0);

...

 /**
 * document which adds .xml extension if not specified
 *
 */
private class FilenameDocument extends PlainDocument {

    @Override
    public void insertString(int offset, String insertedText, AttributeSet set)
    throws BadLocationException {
        if (offset == 0) {
        insertedText = insertedText.trim( );
        }
        super.insertString(offset, insertedText, set);
        if (filenameText != null) {
            final int caretPos = filenameText.getCaretPosition();
            String text = filenameText.getText().trim();
            if (text.indexOf('.') == -1) {
                filenameText.setText(text + ".xml");
                filenameText.setCaretPosition(caretPos);
            }

        }
    }
}

Note that calling setText will result in a recursive call to insertString (), so make sure you have a stop condition.

0
source

I am surprised that no one answered this, but would it not be better to implement an editable JSpinner with SpinnerListModel ?

0
source

Source: https://habr.com/ru/post/1762260/


All Articles