List of usage examples for javax.swing JTextField getCaretPosition
@Transient public int getCaretPosition()
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Popup Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextField textField = new JTextField(); frame.add(textField, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int dotPosition = textField.getCaretPosition(); Rectangle popupLocation = null; try { popupLocation = textField.modelToView(dotPosition); } catch (BadLocationException e) { e.printStackTrace();//from w w w .j av a2 s .c om } System.out.println(popupLocation); } }; KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false); textField.registerKeyboardAction(actionListener, keystroke, JComponent.WHEN_FOCUSED); frame.setSize(250, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Popup Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPopupMenu popup = new JPopupMenu(); JMenuItem menuItem1 = new JMenuItem("Option 1"); popup.add(menuItem1);/*from w ww. jav a 2 s.com*/ JMenuItem menuItem2 = new JMenuItem("Option 2"); popup.add(menuItem2); final JTextField textField = new JTextField(); frame.add(textField, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { try { int dotPosition = textField.getCaretPosition(); Rectangle popupLocation = textField.modelToView(dotPosition); popup.show(textField, popupLocation.x, popupLocation.y); } catch (BadLocationException badLocationException) { System.err.println("Oops"); } } }; KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false); textField.registerKeyboardAction(actionListener, keystroke, JComponent.WHEN_FOCUSED); frame.add(new JLabel("Press '.' to activate Popup menu"), BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); }
From source file:org.openconcerto.erp.core.common.ui.DeviseField.java
public static void addFilteringKeyListener(final JTextField textField) { textField.addKeyListener(new KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent keyEvent) { final char keychar = keyEvent.getKeyChar(); if (keychar == KeyEvent.VK_BACK_SPACE) { return; }//from w w w . j a v a 2 s . c om // pas plus de 2 chiffres apres la virgule int pointPosition = textField.getText().indexOf('.'); if (Character.isDigit(keychar)) { if (pointPosition > -1) { // System.err.println("Text Selected :: " + textField.getSelectedText()); if (textField.getSelectedText() == null) { if (textField.getCaretPosition() <= pointPosition) { return; } else { if (textField.getText().substring(pointPosition).length() <= 2) { return; } } } else { return; } } else { return; } } if (keychar == KeyEvent.VK_PERIOD && textField.getText().indexOf('.') < 0) return; // if (keychar == KeyEvent.VK_MINUS && (textField.getText().indexOf('-') < 0) && // textField.getCaretPosition() == 0) // return; keyEvent.consume(); } }); }