public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { // return immediately when selecting an item if (selecting) { return; } // insert the string into the document super.insertString(offs, str, a); // lookup and select a matching item Object item = lookupItem(getText(0, getLength())); if (item != null) { setSelectedItem(item); } else { // keep old item selected if there is no match item = comboBox.getSelectedItem(); // imitate no insert (later on offs will be incremented by str.length(): selection won't move // forward) offs = offs - str.length(); // provide feedback to the user that his input has been received but can not be accepted comboBox .getToolkit() .beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox); } setText(item.toString()); // select the completed part highlightCompletedText(offs + str.length()); }
@Override public void insertString(final int offs, final String str, final AttributeSet a) throws BadLocationException { // NavigatorLogger.printMessage("Offset:"+offs+" STr:"+str+"L:"+getLength()+"attr:"+a); if ((getLength() + str.length()) <= maxLength) { final char[] source = str.toCharArray(); final char[] result = new char[source.length]; int j = 0; for (int i = 0; i < result.length; i++) { if (Character.isDigit(source[i])) { result[j++] = source[i]; } else { toolkit.beep(); if (log.isDebugEnabled()) { log.debug("insertString: " + source[i]); // NOI18N } } } super.insertString(offs, new String(result, 0, j), a); checked = false; } else { toolkit.beep(); } if ((getLength()) == maxLength) { // getLength() ist schon aktualisiert if (bringFocus2Next == true) { checked = true; nextField.requestFocus(); } // NavigatorLogger.printMessage("Sprung"); // NavigatorLogger.printMessage(nextField); } }
private void setText(String text) { try { // remove all text and insert the completed string super.remove(0, getLength()); super.insertString(0, text, null); } catch (BadLocationException e) { throw new RuntimeException(e.toString()); } }
// The insert string method public void insertString(int offs, String src, AttributeSet a) throws BadLocationException { char[] source = src.toCharArray(); char[] result = new char[source.length]; int j = 0; for (int i = 0; i < result.length; i++) { if (Character.isDigit(source[i])) result[j++] = source[i]; } super.insertString(offs, new String(result, 0, j), a); }
// This method is overriden from the super class. It will be called when // you are trying to insert text in your text component (by typing // or pasting). public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { int currentLength = getLength(); if (currentLength >= maxLength) { // There's not room for more characters. Return. return; } if (currentLength + str.length() > maxLength) { // All of the characters we are trying to insert will not fit. // We must trim the string. str = str.substring(0, maxLength - currentLength); } // Insert the text: super.insertString(offs, str, a); }
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { char[] source = str.toCharArray(); char[] result = new char[source.length]; int j = 0; for (int i = 0; i < result.length; i++) { if (Character.isDigit(source[i])) result[j++] = source[i]; else { toolkit.beep(); // System.err.println("insertString: " + source[i]); } } super.insertString(offs, new String(result, 0, j), a); }
/** * Attempts to insert a string into the document. Produces a system beep if the input does not * represent a positive (or zero) integer. */ public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { StringBuffer intString; // Ignore zero-length and null strings if (str == null || str.length() == 0) return; intString = new StringBuffer(getText(0, getLength())); intString.insert(offset, str); try { int theInt = new Integer(intString.toString()).intValue(); if (theInt < 0 || !allowZero && theInt == 0) { throw new NumberFormatException(); } super.insertString(offset, str, a); } catch (NumberFormatException e) { Toolkit.getDefaultToolkit().beep(); } }
public void insertString(int offset, String str, AttributeSet attSet) throws BadLocationException { if (upperCase) str = str.toUpperCase(); super.insertString(offset, str, attSet); }