本文整理匯總了Java中javax.swing.JTextField.getCaretPosition方法的典型用法代碼示例。如果您正苦於以下問題:Java JTextField.getCaretPosition方法的具體用法?Java JTextField.getCaretPosition怎麽用?Java JTextField.getCaretPosition使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.JTextField
的用法示例。
在下文中一共展示了JTextField.getCaretPosition方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: keyPressed
import javax.swing.JTextField; //導入方法依賴的package包/類
@Override
public void keyPressed(KeyEvent ke) {
int keyCode = ke.getKeyCode();
Object keSource = ke.getSource();
JTextField jTextField = (JTextField) keSource;;
int caretPos = jTextField.getCaretPosition();
int textLength = jTextField.getText().length();
if(keyCode == KeyEvent.VK_ESCAPE){
// Due to a java bug, the callback method is not called automatically when pressing escape.
cancelCellEditing();
} else if(keyCode == KeyEvent.VK_ENTER || keyCode == KeyEvent.VK_TAB){
// Remember which key was used to confirm editing
confirmationKey = keyCode;
} else if(keyCode==KeyEvent.VK_RIGHT && caretPos==textLength){
confirmationKey = keyCode;
stopCellEditing();
} else if(keyCode==KeyEvent.VK_LEFT && caretPos==0){
confirmationKey = keyCode;
stopCellEditing();
}
}
示例2: isCompletionKey
import javax.swing.JTextField; //導入方法依賴的package包/類
private boolean isCompletionKey (int keyCode, JTextField textField) {
if (keyCode == KeyEvent.VK_ENTER || keyCode == KeyEvent.VK_TAB) {
return true;
}
if (keyCode == KeyEvent.VK_RIGHT &&
(textField.getCaretPosition() >= (textField.getDocument().getLength() - 1))) {
return true;
}
return false;
}
示例3: keyTyped
import javax.swing.JTextField; //導入方法依賴的package包/類
public void keyTyped(KeyEvent kT) {
char charackter = kT.getKeyChar();
String singleChar = Character.toString(charackter);
JTextField displayField = (JTextField) kT.getComponent();
String currValue = displayField.getText();
int caretPosition = displayField.getCaretPosition();
// --- Allow negative values ------------------
if (this.isFloatValue==true) {
// --- Float values -----------------------
if (singleChar.equals("-") && countCharsInString(currValue, charackter)<2) {
return;
}
} else {
// --- Integer values ---------------------
if (singleChar.equals("-") && caretPosition==0 && currValue.startsWith("-")==false) {
return;
}
}
if (this.isFloatValue==true) {
// --- Float values -----------------------
if (singleChar.equals(".") || singleChar.equals(",")) {
if (currValue!=null) {
if (currValue.contains(".") || currValue.contains("," )) {
kT.consume();
return;
}
}
} else if (singleChar.equalsIgnoreCase("e")) {
if (currValue!=null) {
if (currValue.contains("e")) {
kT.consume();
return;
}
}
} else if (singleChar.matches( "[0-9]" ) == false) {
kT.consume();
return;
}
} else {
// --- Integer or Long values ---------------------
if ( singleChar.matches( "[0-9]" ) == false ) {
kT.consume();
return;
}
} // --- end if -------------------------------
}