Java Code Examples for javax.swing.text.JTextComponent#addKeyListener()
The following examples show how to use
javax.swing.text.JTextComponent#addKeyListener() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InstantRenameAction.java From netbeans with Apache License 2.0 | 6 votes |
RenameImplementation ( DatabaseItem databaseItem, JTextComponent editor, ASTNode node ) throws BadLocationException { this.editor = editor; document = (NbEditorDocument) editor.getDocument (); elements = getUssages (databaseItem, node); MarkOccurrencesSupport.removeHighlights (editor); if (!elements.isEmpty ()) { SwingUtilities.invokeLater (new Runnable () { public void run () { highlights = new ArrayList<Highlight> (); Highlighting highlighting = Highlighting.getHighlighting (document); Iterator<Element> it = elements.iterator (); while (it.hasNext ()) { Element element = it.next (); ASTItem item = element.getItem (); highlights.add (highlighting.highlight (item.getOffset (), item.getEndOffset (), getHighlightAS ())); } } }); } document.addDocumentListener (this); editor.addKeyListener (this); }
Example 2
Source File: DarkTextFieldUI.java From darklaf with MIT License | 5 votes |
@Override protected void installListeners() { super.installListeners(); JTextComponent c = getComponent(); c.addMouseListener(this); c.addMouseMotionListener(mouseMotionListener); c.addKeyListener(keyListener); }
Example 3
Source File: EntityResourcesSetupPanelVisual.java From netbeans with Apache License 2.0 | 5 votes |
private void addComboBoxListener(JComboBox comboBox) { JTextComponent text = ((JTextComponent) comboBox.getEditor().getEditorComponent()); text.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent event) { //updatePreview(); } }); }
Example 4
Source File: HintsUI.java From netbeans with Apache License 2.0 | 5 votes |
private void register() { JTextComponent comp = getComponent(); if (comp == null) { return; } comp.addKeyListener (this); comp.addCaretListener(this); }
Example 5
Source File: PopupManager.java From netbeans with Apache License 2.0 | 5 votes |
/** Creates a new instance of PopupManager */ public PopupManager(JTextComponent textComponent) { this.textComponent = textComponent; keyListener = new PopupKeyListener(); textComponent.addKeyListener(keyListener); componentListener = new TextComponentListener(); textComponent.addComponentListener(componentListener); }
Example 6
Source File: CompletionLayoutPopup.java From netbeans with Apache License 2.0 | 5 votes |
@Override public void setEditorComponent(JTextComponent comp) { JTextComponent thisComp = getEditorComponent(); boolean change = thisComp != comp; if (thisComp != null && change) { thisComp.removeKeyListener(getChKeyListener()); } super.setEditorComponent(comp); if (comp != null && change) { comp.addKeyListener(getChKeyListener()); } }
Example 7
Source File: AbbrevDetection.java From netbeans with Apache License 2.0 | 5 votes |
private AbbrevDetection(JTextComponent component) { this.component = component; component.addCaretListener(this); doc = component.getDocument(); if (doc != null) { listenOnDoc(); } String mimeType = DocumentUtilities.getMimeType(component); if (mimeType != null) { mimePath = MimePath.parse(mimeType); prefs = MimeLookup.getLookup(mimePath).lookup(Preferences.class); prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, this, prefs)); } // Load the settings preferenceChange(null); component.addKeyListener(this); component.addPropertyChangeListener(this); surroundsWithTimer = new Timer(0, new ActionListener() { public void actionPerformed(ActionEvent e) { // #124515, give up when the document is locked otherwise we are likely // to cause a deadlock. if (!DocumentUtilities.isReadLocked(doc)) { showSurroundWithHint(); } } }); surroundsWithTimer.setRepeats(false); }
Example 8
Source File: InstantRefactoringPerformer.java From netbeans with Apache License 2.0 | 4 votes |
public InstantRefactoringPerformer(final JTextComponent target, int caretOffset, InstantRefactoringUI ui) { releaseAll(); this.target = target; this.ui = ui; doc = target.getDocument(); MutablePositionRegion mainRegion = null; List<MutablePositionRegion> regions = new ArrayList<>(ui.getRegions().size()); for (MutablePositionRegion current : ui.getRegions()) { // TODO: type parameter name is represented as ident -> ignore surrounding <> in rename if (isIn(current, caretOffset)) { mainRegion = current; } else { regions.add(current); } } if (mainRegion == null) { throw new IllegalArgumentException("No highlight contains the caret."); } regions.add(0, mainRegion); region = new SyncDocumentRegion(doc, regions); if (doc instanceof BaseDocument) { BaseDocument bdoc = ((BaseDocument) doc); bdoc.addPostModificationDocumentListener(this); UndoableWrapper wrapper = MimeLookup.getLookup("text/x-java").lookup(UndoableWrapper.class); if(wrapper != null) { wrapper.setActive(true, this); } UndoableEdit undo = new CancelInstantRenameUndoableEdit(this); for (UndoableEditListener l : bdoc.getUndoableEditListeners()) { l.undoableEditHappened(new UndoableEditEvent(doc, undo)); } } target.addKeyListener(this); target.putClientProperty(InstantRefactoringPerformer.class, this); target.putClientProperty("NetBeansEditor.navigateBoundaries", mainRegion); // NOI18N requestRepaint(); target.select(mainRegion.getStartOffset(), mainRegion.getEndOffset()); span = region.getFirstRegionLength(); compl = new CompletionLayout(this); compl.setEditorComponent(target); final KeyStroke OKKS = ui.getKeyStroke(); target.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(OKKS, OKActionKey); Action OKAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent ae) { if(registry.contains(InstantRefactoringPerformer.this)) { doFullRefactoring(); target.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).remove(OKKS); target.getActionMap().remove(OKActionKey); } else { target.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).remove(OKKS); target.getActionMap().remove(OKActionKey); } } }; target.getActionMap().put(OKActionKey, OKAction); final KeyStroke keyStroke = ui.getKeyStroke(); compl.showCompletion(ui.getOptions(), caretOffset, Bundle.INFO_PressAgain(getKeyStrokeAsText(keyStroke))); registry.add(this); }
Example 9
Source File: InstantRenamePerformer.java From netbeans with Apache License 2.0 | 4 votes |
/** Creates a new instance of InstantRenamePerformer */ private InstantRenamePerformer(JTextComponent target, Set<Token> highlights, int caretOffset) throws BadLocationException { this.target = target; doc = target.getDocument(); MutablePositionRegion mainRegion = null; List<MutablePositionRegion> regions = new ArrayList<MutablePositionRegion>(); for (Token h : highlights) { // type parameter name is represented as ident -> ignore surrounding <> in rename int delta = h.id() == JavadocTokenId.IDENT && h.text().charAt(0) == '<' && h.text().charAt(h.length() - 1) == '>' ? 1 : 0; Position start = NbDocument.createPosition(doc, h.offset(null) + delta, Bias.Backward); Position end = NbDocument.createPosition(doc, h.offset(null) + h.length() - delta, Bias.Forward); MutablePositionRegion current = new MutablePositionRegion(start, end); if (isIn(current, caretOffset)) { mainRegion = current; } else { regions.add(current); } } if (mainRegion == null) { throw new IllegalArgumentException("No highlight contains the caret."); } regions.add(0, mainRegion); region = new SyncDocumentRegion(doc, regions); if (doc instanceof BaseDocument) { BaseDocument bdoc = ((BaseDocument) doc); bdoc.setPostModificationDocumentListener(this); UndoableEdit undo = new CancelInstantRenameUndoableEdit(this); for (UndoableEditListener l : bdoc.getUndoableEditListeners()) { l.undoableEditHappened(new UndoableEditEvent(doc, undo)); } } target.addKeyListener(this); target.putClientProperty(InstantRenamePerformer.class, this); target.putClientProperty("NetBeansEditor.navigateBoundaries", mainRegion); // NOI18N requestRepaint(); target.select(mainRegion.getStartOffset(), mainRegion.getEndOffset()); span = region.getFirstRegionLength(); registry.add(this); sendUndoableEdit(doc, CloneableEditorSupport.BEGIN_COMMIT_GROUP); }
Example 10
Source File: InstantRenamePerformer.java From netbeans with Apache License 2.0 | 4 votes |
/** Creates a new instance of InstantRenamePerformer */ private InstantRenamePerformer(JTextComponent target, Set<OffsetRange> highlights, int caretOffset) throws BadLocationException { this.target = target; doc = target.getDocument(); MutablePositionRegion mainRegion = null; List<MutablePositionRegion> regions = new ArrayList<MutablePositionRegion>(); for (OffsetRange h : highlights) { Position start = NbDocument.createPosition(doc, h.getStart(), Bias.Backward); Position end = NbDocument.createPosition(doc, h.getEnd(), Bias.Forward); MutablePositionRegion current = new MutablePositionRegion(start, end); if (isIn(current, caretOffset)) { mainRegion = current; } else { regions.add(current); } } if (mainRegion == null) { Logger.getLogger(InstantRenamePerformer.class.getName()).warning("No highlight contains the caret (" + caretOffset + "; highlights=" + highlights + ")"); //NOI18N // Attempt to use another region - pick the one closest to the caret if (regions.size() > 0) { mainRegion = regions.get(0); int mainDistance = Integer.MAX_VALUE; for (MutablePositionRegion r : regions) { int distance = caretOffset < r.getStartOffset() ? (r.getStartOffset()-caretOffset) : (caretOffset-r.getEndOffset()); if (distance < mainDistance) { mainRegion = r; mainDistance = distance; } } } else { return; } } regions.add(0, mainRegion); region = new SyncDocumentRegion(doc, regions); if (doc instanceof BaseDocument) { ((BaseDocument) doc).addPostModificationDocumentListener(this); } target.addKeyListener(this); target.putClientProperty("NetBeansEditor.navigateBoundaries", mainRegion); // NOI18N target.putClientProperty(InstantRenamePerformer.class, this); requestRepaint(); target.select(mainRegion.getStartOffset(), mainRegion.getEndOffset()); }
Example 11
Source File: PaymentPanel.java From Java-Simple-Hotel-Management with GNU General Public License v3.0 | 4 votes |
public void searchHelper() { final DefaultComboBoxModel model = new DefaultComboBoxModel(bookingList); combo_booking.setModel(model); JTextComponent editor = (JTextComponent) combo_booking.getEditor().getEditorComponent(); editor.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent evt) { if (evt.getKeyChar() == KeyEvent.VK_ENTER) { String details = (String) combo_booking.getSelectedItem(); //System.out.println(details); if (!details.contains(",")) { JOptionPane.showMessageDialog(null, "no booking found, try adding a new booking"); } else { bookingId = Integer.parseInt(details.substring(details.lastIndexOf(",") + 1)); //tf_bookingId.setText(bookinId+""); // A if condition should be here, but not required as the last line has no chance of returning -1. } } /// suggestion generation String value = ""; try { value = combo_booking.getEditor().getItem().toString(); // System.out.println(value +" <<<<<<<<<<<<<"); } catch (Exception ex) { } if (value.length() >= 2) { // System.out.println("working"); bookingComboFill(bookingdB.bookingsReadyForOrder(value)); // bookingdB.flushAll(); } } }); }
Example 12
Source File: OrderPanel.java From Java-Simple-Hotel-Management with GNU General Public License v3.0 | 4 votes |
public void searchHelper() { final DefaultComboBoxModel model = new DefaultComboBoxModel(bookingList); combo_booking.setModel(model); JTextComponent editor = (JTextComponent) combo_booking.getEditor().getEditorComponent(); editor.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent evt) { if(evt.getKeyChar() == KeyEvent.VK_ENTER) { String details = (String) combo_booking.getSelectedItem(); //System.out.println(details); if(!details.contains(",")) { JOptionPane.showMessageDialog(null, "no booking found, try adding a new booking"); } else { int bookinId = Integer.parseInt(details.substring(details.lastIndexOf(",")+1)); tf_bookingId.setText(bookinId+""); // A if condition should be here, but not required as the last line has no chance of returning -1. } } /// suggestion generation String value = ""; try { value = combo_booking.getEditor().getItem().toString(); // System.out.println(value +" <<<<<<<<<<<<<"); } catch (Exception ex) { } if (value.length() >= 2) { // System.out.println("working"); bookingComboFill(db.bookingsReadyForOrder(value)); db.flushAll(); } } }); }
Example 13
Source File: ControlPanel.java From Java-Simple-Hotel-Management with GNU General Public License v3.0 | 4 votes |
private void searchCustomerHelper() { final DefaultComboBoxModel model = new DefaultComboBoxModel(customerList); combo_users.setModel(model); JTextComponent editor = (JTextComponent) combo_users.getEditor().getEditorComponent(); editor.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent evt) { //System.out.println((int)KeyEvent.VK_ACCEPT); // .............. 1st part for searching a customer, user should hit enter if he finds a user. if (evt.getKeyChar() == KeyEvent.VK_ENTER) { String details = (String) combo_users.getSelectedItem(); //System.out.println(details); if (!details.contains(",")) { JOptionPane.showMessageDialog(null, "no users found, try adding a new user"); } else { int customerId = Integer.parseInt(details.substring(details.lastIndexOf(",") + 1)); // A if condition should be here, but not required as the last line has no chance of returning -1. System.out.println(".>> " + customerId); ResultSet userDB = db.searchAnUser(customerId); displayTextField(userDB); existingCustomer = true; } } //................. end of first part, start of 2nd part, for suggesting saved customer data list String value = ""; try { value = combo_users.getEditor().getItem().toString(); //System.out.println(value +" <<<<<<<<<<<<<"); } catch (Exception ex) { } if (value.length() >= 2) { //System.out.println("working"); userComboFill(db.searchUser(value)); } } }); }
Example 14
Source File: AutoCompleteDecorator.java From cropplanning with GNU General Public License v3.0 | 4 votes |
/** * Enables automatic completion for the given JComboBox. The automatic * completion will be strict (only items from the combo box can be selected) * if the combo box is not editable. * @param comboBox a combo box * @param stringConverter the converter used to transform items to strings */ public static void decorate(final JComboBox comboBox, final ObjectToStringConverter stringConverter) { boolean strictMatching = !comboBox.isEditable(); // has to be editable comboBox.setEditable(true); // fix the popup location AquaLnFPopupLocationFix.install(comboBox); // configure the text component=editor component JTextComponent editorComponent = (JTextComponent) comboBox.getEditor().getEditorComponent(); final AbstractAutoCompleteAdaptor adaptor = new ComboBoxAdaptor(comboBox); final AutoCompleteDocument document = new AutoCompleteDocument(adaptor, strictMatching, stringConverter); decorate(editorComponent, document, adaptor); // show the popup list when the user presses a key final KeyListener keyListener = new KeyAdapter() { public void keyPressed(KeyEvent keyEvent) { // don't popup on action keys (cursor movements, etc...) if (keyEvent.isActionKey()) return; // don't popup if the combobox isn't visible anyway if (comboBox.isDisplayable() && !comboBox.isPopupVisible()) { int keyCode = keyEvent.getKeyCode(); // don't popup when the user hits shift,ctrl or alt if (keyCode==keyEvent.VK_SHIFT || keyCode==keyEvent.VK_CONTROL || keyCode==keyEvent.VK_ALT) return; // don't popup when the user hits escape (see issue #311) if (keyCode==keyEvent.VK_ESCAPE) return; comboBox.setPopupVisible(true); } } }; editorComponent.addKeyListener(keyListener); if (stringConverter!=ObjectToStringConverter.DEFAULT_IMPLEMENTATION) { comboBox.setEditor(new AutoCompleteComboBoxEditor(comboBox.getEditor(), stringConverter)); } // Changing the l&f can change the combobox' editor which in turn // would not be autocompletion-enabled. The new editor needs to be set-up. comboBox.addPropertyChangeListener("editor", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { ComboBoxEditor editor = (ComboBoxEditor) e.getNewValue(); if (editor!=null && editor.getEditorComponent()!=null) { if (!(editor instanceof AutoCompleteComboBoxEditor) && stringConverter!=ObjectToStringConverter.DEFAULT_IMPLEMENTATION) { comboBox.setEditor(new AutoCompleteComboBoxEditor(editor, stringConverter)); // Don't do the decorate step here because calling setEditor will trigger // the propertychange listener a second time, which will do the decorate // and addKeyListener step. } else { decorate((JTextComponent) editor.getEditorComponent(), document, adaptor); editor.getEditorComponent().addKeyListener(keyListener); } } } }); }