DocumentFilter ?它給你操縱的能力。
[http://www.java2s.com/Tutorial/Java/0240__Swing/FormatJTextFieldstexttouppercase.htm]
對不起。Ĵ我用Jython(在Java中的Python) - 但很容易理解
# 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));
警告/類型轉換的良好格式。同樣的模式將有助於處理雙金額(銷售數字/價格輸入或顯示) – 2014-10-13 14:31:51
它工作正常,但我有一個查詢,當我插入一些文本在文本字段中,然後我想調用一個方法。我不知道它是如何完成的。 – 2015-03-14 11:21:43
當我點擊另一個表格單元格時,我遇到了JTable沒有從可編輯的JComboBox獲取文本框更新的問題,並且這裏的insertUpdate函數是使它正常工作的唯一方法。 – winchella 2015-10-07 17:42:06