Value Change Listener to JTextField

前端 未结 12 1960
情歌与酒
情歌与酒 2020-11-22 04:16

I want the message box to appear immediately after the user changes the value in the textfield. Currently, I need to hit the enter key to get the message box to pop out. Is

12条回答
  •  长情又很酷
    2020-11-22 04:33

    DocumentFilter ? It gives you the ability to manipulate.

    [ http://www.java2s.com/Tutorial/Java/0240__Swing/FormatJTextFieldstexttouppercase.htm ]

    Sorry. J am using Jython (Python in Java) - but easy to understand

    # python style
    # upper chars [ text.upper() ]
    
    class myComboBoxEditorDocumentFilter( DocumentFilter ):
    def __init__(self,jtext):
        self._jtext = jtext
    
    def insertString(self,FilterBypass_fb, offset, text, AttributeSet_attrs):
        txt = self._jtext.getText()
        print('DocumentFilter-insertString:',offset,text,'old:',txt)
        FilterBypass_fb.insertString(offset, text.upper(), AttributeSet_attrs)
    
    def replace(self,FilterBypass_fb, offset, length, text, AttributeSet_attrs):
        txt = self._jtext.getText()
        print('DocumentFilter-replace:',offset, length, text,'old:',txt)
        FilterBypass_fb.replace(offset, length, text.upper(), AttributeSet_attrs)
    
    def remove(self,FilterBypass_fb, offset, length):
        txt = self._jtext.getText()
        print('DocumentFilter-remove:',offset, length, 'old:',txt)
        FilterBypass_fb.remove(offset, length)
    
    // (java style ~example for ComboBox-jTextField)
    cb = new ComboBox();
    cb.setEditable( true );
    cbEditor = cb.getEditor();
    cbEditorComp = cbEditor.getEditorComponent();
    cbEditorComp.getDocument().setDocumentFilter(new myComboBoxEditorDocumentFilter(cbEditorComp));
    

提交回复
热议问题