Java Utililty Methods JTextComponent

List of utility methods to do JTextComponent

Description

The list of methods to do JTextComponent are organized into topic(s).

Method

voidreadOnly(JTextComponent c, Component parent)
read Only
c.setEditable(false);
c.setForeground(parent.getForeground());
c.setBackground(parent.getBackground());
voidrefreshJTextComponent(JTextComponent textComponent)
refresh J Text Component
int pos = textComponent.getCaretPosition();
textComponent.setText(textComponent.getText());
textComponent.validate();
try {
    textComponent.setCaretPosition(pos);
} catch (IllegalArgumentException e) {
    System.err.println(e.getMessage());
voidregisterEvents(final JTextComponent textCmp, final String hintText)
register Events
textCmp.addFocusListener(new FocusListener() {
    public void focusGained(FocusEvent e) {
        if (textCmp.getText().equals(hintText)) {
            textCmp.setText("");
    public void focusLost(FocusEvent e) {
});
voidregisterUpperCase(final JTextComponent textComp)
register Upper Case
textComp.addKeyListener(new KeyAdapter() {
    @Override
    public void keyReleased(KeyEvent e) {
        textComp.setText(textComp.getText().toUpperCase());
});
voidremoveAllHighlightsUsingPainter(final JTextComponent textComponent, final Highlighter.HighlightPainter painter)
remove All Highlights Using Painter
Highlighter highlighter = textComponent.getHighlighter();
for (Highlighter.Highlight highlight : highlighter.getHighlights()) {
    if (highlight.getPainter() == painter) {
        highlighter.removeHighlight(highlight);
voidremoveDocumentListeners(JTextComponent component)
Remove all document listeners from this text component
Document document = component.getDocument();
if (document instanceof AbstractDocument) {
    DocumentListener[] dls = ((AbstractDocument) document).getDocumentListeners();
    for (int i = 0; i < dls.length; i++) {
        ((AbstractDocument) document).removeDocumentListener(dls[i]);
    UndoableEditListener[] uels = ((AbstractDocument) document).getUndoableEditListeners();
    for (int i = 0; i < uels.length; i++) {
...
StringsafeGetText(JTextComponent textComponent)
Gets the "text" contents of a JTextComponent without the possibility of a NullPointerException .
String value = "";
if ((textComponent != null) && (textComponent.getText() != null)) {
    value = textComponent.getText();
return value;
voidscrollToText(JTextComponent textComponent, int startingIndex, int endingIndex)
Scrolls the specified text component so the text between the starting and ending index are visible.
try {
    Rectangle startingRectangle = textComponent.modelToView(startingIndex);
    Rectangle endDingRectangle = textComponent.modelToView(endingIndex);
    Rectangle totalBounds = startingRectangle.union(endDingRectangle);
    textComponent.scrollRectToVisible(totalBounds);
    textComponent.repaint();
} catch (BadLocationException e) {
    e.printStackTrace();
...
voidscrollToTop(JTextComponent edit1)
scroll To Top
edit1.setCaretPosition(0);
voidsetError(JTextComponent component, boolean error)
set Error
component.setBackground(error ? ERROR_BG : NORMAL_BG);