本文整理汇总了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 -------------------------------
}