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

voidcurrentLineChanged(final JTextComponent c)
Fetches the previous caret location, stores the current caret location.
if (null == c.getClientProperty(PREVIOUS_CARET)) {
    return;
try {
    final int previousCaret = ((Integer) c.getClientProperty(PREVIOUS_CARET)).intValue();
    final Rectangle prev = c.modelToView(previousCaret);
    final Rectangle r = c.modelToView(c.getCaretPosition());
    c.putClientProperty(PREVIOUS_CARET, new Integer(c.getCaretPosition()));
...
voidenableJTextField(JTextComponent component, boolean enable, Color color)
enable J Text Field
component.setEnabled(enable);
component.setEditable(enable);
component.setBackground(color);
component.invalidate();
component.validate();
component.repaint(1);
voidensureCustomBackgroundStored(JTextComponent comp)
Ensures that a text component's custom background - if any - is stored as a client property.
if (getStoredBackground(comp) != null)
    return;
Color background = comp.getBackground();
if ((background == null) || (background instanceof UIResource) || (background == WARNING_BACKGROUND)
        || (background == ERROR_BACKGROUND))
    return;
comp.putClientProperty(STORED_BACKGROUND_KEY, background);
voidensureCustomBackgroundStored(JTextComponent comp)
Ensures that a text component's custom background - if any - is stored as a client property.
if (getStoredBackground(comp) != null)
    return;
Color background = comp.getBackground();
if ((background == null) || (background instanceof UIResource) || (background == WARNING_BACKGROUND)
        || (background == ERROR_BACKGROUND))
    return;
comp.putClientProperty(STORED_BACKGROUND_KEY, background);
voidfillWith(String value, JTextComponent... fields)
fill With
for (JTextComponent field : fields) {
    field.setText(value);
intgetAdjustedClickCount(JTextComponent comp, MouseEvent e)
Return the MouseEvent's click count, possibly reduced by the value of the component's SKIP_CLICK_COUNT client property.
int cc = e.getClickCount();
if (cc == 1) {
    comp.putClientProperty(SKIP_CLICK_COUNT, null);
} else {
    Integer sub = (Integer) comp.getClientProperty(SKIP_CLICK_COUNT);
    if (sub != null) {
        return cc - sub;
return cc;
StringgetCurrentTextBlock(JTextComponent textComponent)
this method will get the current block of text that the caret is within.
String patternStr = "(?m)^\\s*?$";
Pattern pattern = Pattern.compile(patternStr);
String text = textComponent.getText();
Matcher matcher = pattern.matcher(text);
int caretPosition = textComponent.getCaretPosition();
int prevBlankLineIndex = 0;
while (matcher.find()) {
    if (matcher.start() >= caretPosition)
...
intgetNumberOfLines(final JTextComponent component)
get Number Of Lines
final Element root = component.getDocument().getDefaultRootElement();
return root.getElementCount();
ElementgetParagraphElement(JTextComponent c, int offs)
Determines the element to use for a paragraph/line.
Document doc = c.getDocument();
if (doc instanceof StyledDocument) {
    return ((StyledDocument) doc).getParagraphElement(offs);
Element map = doc.getDefaultRootElement();
int index = map.getElementIndex(offs);
Element paragraph = map.getElement(index);
if ((offs >= paragraph.getStartOffset()) && (offs < paragraph.getEndOffset())) {
...
intgetPositionBelow(JTextComponent c, int offs, int x)
Determines the position in the model that is closest to the given view location in the row below.
int lastOffs = getRowEnd(c, offs) + 1;
if (lastOffs <= 0) {
    return -1;
int bestSpan = Short.MAX_VALUE;
int n = c.getDocument().getLength();
int y = 0;
Rectangle r = null;
...