/ Published in: Java
This is a modification of the Sun tutorial code that initializes the text editor to accept words that are not part of the auto-complete list. In the terms of the implementation, it is referred to as "not strict".
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */ import java.util.List; import javax.swing.JTextField; import javax.swing.text.*; super.remove(i, j); insertString(i, s, attributeset); } if (s == null || "".equals(s)) return; int j = (i + s.length()) - 1; if (isStrict && s2 == null) { s2 = getMatch(s1); j--; } else if (!isStrict && s2 == null) { super.insertString(i, s, attributeset); return; } if (autoComboBox != null && s2 != null) autoComboBox.setSelectedValue(s2); super.remove(0, getLength()); super.insertString(0, s2, attributeset); setSelectionStart(j + 1); setSelectionEnd(getLength()); } int k = getSelectionStart(); if (k > 0) k--; if (!isStrict && s == null) { super.remove(i, j); } else { super.remove(0, getLength()); super.insertString(0, s, null); } if (autoComboBox != null && s != null) autoComboBox.setSelectedValue(s); try { setSelectionStart(k); setSelectionEnd(getLength()); } } } isCaseSensitive = false; isStrict = false; autoComboBox = null; if (list == null) { } else { dataList = list; init(); return; } } isCaseSensitive = false; isStrict = false; autoComboBox = null; if (list == null) { } else { dataList = list; autoComboBox = b; init(); return; } } private void init() { setDocument(new AutoDocument()); if (isStrict && dataList.size() > 0) setText(dataList.get(0).toString()); } for (int i = 0; i < dataList.size(); i++) { if (s1 != null) { if (!isCaseSensitive && s1.toLowerCase().startsWith(s.toLowerCase())) return s1; if (isCaseSensitive && s1.startsWith(s)) return s1; } } return null; } AutoDocument _lb = (AutoDocument) getDocument(); if (_lb != null) try { _lb.replace(i, j - i, s, null); } } public boolean isCaseSensitive() { return isCaseSensitive; } public void setCaseSensitive(boolean flag) { isCaseSensitive = flag; } public boolean isStrict() { return isStrict; } public void setStrict(boolean flag) { isStrict = flag; } return dataList; } if (list == null) { } else { dataList = list; return; } } private boolean isCaseSensitive; private boolean isStrict; private Java2sAutoComboBox autoComboBox; }