Java JTextComponent Caret makeCaretAlwaysVisible(final JTextComponent comp)

Here you can find the source of makeCaretAlwaysVisible(final JTextComponent comp)

Description

Make caret visible even when the JTextComponent is not editable.

License

Open Source License

Declaration

public static FocusListener makeCaretAlwaysVisible(final JTextComponent comp) 

Method Source Code

//package com.java2s;
/**************************************************************************
 OmegaT - Computer Assisted Translation (CAT) tool 
  with fuzzy matching, translation memory, keyword search, 
  glossaries, and translation leveraging into updated projects.
    /*from  www .  ja v a  2 s  . c o  m*/
 Copyright (C) 2006 Henry Pijffers
       2013 Yu Tang
       2014-2015 Aaron Madlon-Kay
       Home page: http://www.omegat.org/
       Support center: http://groups.yahoo.com/group/OmegaT/
    
 This file is part of OmegaT.
    
 OmegaT is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 OmegaT is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************/

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.text.Caret;

import javax.swing.text.JTextComponent;

public class Main {
    /**
     * Make caret visible even when the {@link JTextComponent} is not editable.
     */
    public static FocusListener makeCaretAlwaysVisible(final JTextComponent comp) {
        FocusListener listener = new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                Caret caret = comp.getCaret();
                caret.setVisible(true);
                caret.setSelectionVisible(true);
            }
        };
        comp.addFocusListener(listener);
        return listener;
    }
}

Related

  1. getCaretCol(int idx, JTextComponent comp)
  2. getCaretRow(int idx, JTextComponent comp)
  3. getLineAtCaret(final JTextComponent component)
  4. selectLineAtCaret(JTextComponent editor)
  5. setCaretUpdateEnabled(JTextComponent comp, boolean updateEnabled)
  6. setTextAndResetCaret(JTextComponent component, String text)