Java Code Examples for javax.swing.text.JTextComponent#addFocusListener()

The following examples show how to use javax.swing.text.JTextComponent#addFocusListener() . 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: FileNameController.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void init() {

        Component cboxEditorComp = component.getEditor().getEditorComponent();
        fileNamePatternEditor = (JTextComponent) cboxEditorComp;
        fileNamePatternWatcher =
                new FileNamePatternWatcher(fileNamePatternEditor);
        fileNamePatternEditor.addFocusListener(fileNamePatternWatcher);
        fileNamePatternEditor.addHierarchyListener(fileNamePatternWatcher);
        fileNamePatternEditor.getDocument().addDocumentListener(
                new FileNameChangeListener());
        defaultColor = component.getForeground();
        component.setEditable(true);
        List<String> entries = FindDialogMemory.getDefault().getFileNamePatterns();
        if (!entries.isEmpty()) {
            component.setModel(new ListComboBoxModel<String>(entries, true));
        }
    }
 
Example 2
Source File: HintsUI.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void propertyChange(PropertyChangeEvent e) {
    JTextComponent active = EditorRegistry.lastFocusedComponent();
    
    if (getComponent() != active) {
        removeHints();
        setComponent(active);

        if (getComponent() != null) {
            getComponent().removeFocusListener(this);
        }

        if (active != null) {
            active.addFocusListener(this);
        }
    }
}
 
Example 3
Source File: GhostText.java    From aurous-app with GNU General Public License v2.0 6 votes vote down vote up
public GhostText(final String text, final JTextComponent component,
		final Show show) {
	this.component = component;
	setShow(show);
	this.document = component.getDocument();

	setText(text);

	setFont(component.getFont());

	setForeground(Color.LIGHT_GRAY);
	setBorder(new EmptyBorder(component.getInsets()));
	setHorizontalAlignment(SwingConstants.LEFT);

	component.addFocusListener(this);
	this.document.addDocumentListener(this);

	component.setLayout(new BorderLayout());
	component.add(this);
	checkForPrompt();
}
 
Example 4
Source File: TextPrompt.java    From magarena with GNU General Public License v3.0 6 votes vote down vote up
public TextPrompt(String text, JTextComponent component, Show show)
{
    this.component = component;
    setShow( show );
    document = component.getDocument();

    setText( text );
    setFont( component.getFont() );
    setForeground( component.getForeground() );
    setBorder(null);
    setHorizontalAlignment(JLabel.LEADING);

    component.addFocusListener( this );
    document.addDocumentListener( this );

    component.setLayout( new BorderLayout() );
    component.add( this );
    checkForPrompt();
}
 
Example 5
Source File: TreeTableCellEditorImpl.java    From ganttproject with GNU General Public License v3.0 6 votes vote down vote up
private static Runnable createOnFocusGained(final JTextComponent textComponent, final Runnable onFocusGained) {
  final FocusListener focusListener = new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent arg0) {
      super.focusGained(arg0);
      SwingUtilities.invokeLater(onFocusGained);
      textComponent.removeFocusListener(this);
    }
  };
  textComponent.addFocusListener(focusListener);
  return new Runnable() {
    @Override
    public void run() {
      textComponent.requestFocus();
    }
  };
}
 
Example 6
Source File: NbEditorUI.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected @Override void installUI(JTextComponent c) {
    super.installUI(c);

    if (!attached){
        attachSystemActionPerformer("find");
        attachSystemActionPerformer("replace");
        attachSystemActionPerformer(ExtKit.gotoAction);
        attachSystemActionPerformer(ExtKit.showPopupMenuAction);

        // replacing DefaultEditorKit.deleteNextCharAction by BaseKit.removeSelectionAction
        // #41223
        // attachSystemActionPerformer(BaseKit.removeSelectionAction);
        
        attached = true;
    }
    
    c.addFocusListener(focusL);
}
 
Example 7
Source File: SuggestDecorator.java    From otroslogviewer with Apache License 2.0 6 votes vote down vote up
/**
 * Add popup with suggestion to text component
 * @param textComponent  text component
 * @param suggestionSource source of suggestions
 * @param suggestionRenderer renderer for suggestions
 * @param selectionListener suggestion listener to be executed after suggestion is selected
 * @param clearFocusAfterSelection true if text selection should be removed and caret set to end of text after selecting suggestion
 * @param <T> Suggestion type
 */
public static <T> void decorate(final JTextComponent textComponent,
                                SuggestionSource<T> suggestionSource,
                                SuggestionRenderer<T> suggestionRenderer,
                                SelectionListener<T> selectionListener,
                                boolean clearFocusAfterSelection) {

  Document document = textComponent.getDocument();
  SuggestionDocumentListener<? extends T> listener = new SuggestionDocumentListener<>(textComponent, suggestionSource, suggestionRenderer, selectionListener);
  document.addDocumentListener(listener);
  if (clearFocusAfterSelection) {
    textComponent.addFocusListener(new FocusAdapter() {
      @Override
      public void focusGained(FocusEvent e) {
        //do not select all on OSX after suggestion is selected
        if (e.getOppositeComponent() == null) {
          clearTextFieldSelectionAsync(textComponent);
        }
      }
    });
  }
}
 
Example 8
Source File: JTextObserver.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds this instance as a listener for the specified text component.
 * 
 * @param t  the text component.
 */
public static void addTextComponent(final JTextComponent t) {
    if (singleton == null) {
        singleton = new JTextObserver();
    }
    t.addFocusListener(singleton);
}
 
Example 9
Source File: DarculaTextFieldUI.java    From Darcula with Apache License 2.0 5 votes vote down vote up
@Override
protected void installListeners() {
  super.installListeners();
  final JTextComponent c = getComponent();
  c.addFocusListener(myFocusListener);
  c.addMouseMotionListener(myMouseMotionListener);
  c.addMouseListener(myMouseListener);
}
 
Example 10
Source File: SeaGlassTextFieldUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
/**
 * @see javax.swing.plaf.basic.BasicTextUI#installDefaults()
 */
protected void installDefaults() {
    // Installs the text cursor on the component
    super.installDefaults();

    JTextComponent c = getComponent();

    updateStyle(c);
    c.addFocusListener(this);
}
 
Example 11
Source File: DialogEditor.java    From ET_Redux with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param textComp
 * @param editable
 */
public UnDoAbleDocument(JTextComponent textComp, boolean editable) {
    this(textComp);
    textComp.setEditable(editable);
    if (editable) {
        textComp.setBackground(ReduxConstants.myEditingWhiteColor);
    } else {
        textComp.setBackground(ReduxConstants.myNotEditingGreyColor);
    }

    textComp.addFocusListener(new SelectTextFocusListener());
}
 
Example 12
Source File: FindReplaceUtility.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * @param textComponent the text component to listen to
 */
public static void registerTextComponent(JTextComponent textComponent) {
    textComponent.addFocusListener(TEXT_FOCUS_LISTENER);
}
 
Example 13
Source File: AnimationAutoCompletion.java    From 3Dscript with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void addTo(JTextComponent tc) {
	tc.addFocusListener(this);
	tc.getDocument().addDocumentListener(this);
	tc.addCaretListener(this);
}
 
Example 14
Source File: AnimationAutoCompletion.java    From 3Dscript with BSD 2-Clause "Simplified" License 4 votes vote down vote up
void addTo(JTextComponent tc) {
	tc.addFocusListener(this);
	tc.addHierarchyListener(this);
}
 
Example 15
Source File: SectionInnerPanel.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/** Adds text component to the set of JTextComponents that should be validated for correctness.
 * After the value in this component is changed either setValue() method is called(value is correct)
 * or rollbackValue() method is called(value is incorrect). Also the documentChanged() method is called during editing.
 * @param tc JTextComponent whose content is related to data model and should be validated before saving to data model.
 */
public final void addValidatee(final JTextComponent tc) {
    tc.getDocument().addDocumentListener(new TextListener(tc));
    tc.addFocusListener(new ValidateFocusListener(tc));
}
 
Example 16
Source File: SectionInnerPanel.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/** Adds text component to the set of JTextComponents that modifies the model.
 * After the value in this component is changed the setValue() method is called.
 *
 * @param tc JTextComponent whose content is related to data model
 * @param test indicates whether the original value should be tested against the current value when focus is lost
 */
public final void addModifier(final JTextComponent tc, boolean test) {
    tc.addFocusListener(new ModifyFocusListener(tc, test));
}
 
Example 17
Source File: SectionInnerPanel.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/** Adds text component to the set of JTextComponents that modifies the model.
 * After the value in this component is changed the setValue() method is called.
 * @param tc JTextComponent whose content is related to data model
 */
public final void addModifier(final JTextComponent tc) {
    tc.addFocusListener(new ModifyFocusListener(tc));
}