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

booleanisEmpty(JTextComponent component)
is Empty
String content = component.getText();
return null == content || content.equals("");
booleanisJavaContext(final JTextComponent component, final int offset, final boolean allowInStrings)
is Java Context
return true;
booleanisNonWhitespaceBetween(JTextComponent editor, int iStart, int iEnd)
is Non Whitespace Between
if (iStart > iEnd) {
    int iTemp = iEnd;
    iEnd = iStart;
    iStart = iTemp;
try {
    String strText = editor.getText(iStart, iEnd - iStart);
    return strText.trim().length() > 0;
...
voidloadFileToPane(String fname, JTextComponent pane)
load File To Pane
FileReader fr = null;
try {
    fr = new FileReader(fname);
    pane.read(fr, null);
    fr.close();
} catch (IOException ex) {
    mylogger.log(Level.SEVERE, "Error while loading: ", ex);
} finally {
...
voidloadTextToPane(String text, JTextComponent pane, boolean append)
load Text To Pane
if (append) {
    ((JTextArea) pane).append(text);
} else {
    ((JTextArea) pane).setText(text);
voidmakeVisible(JTextComponent textPane, int start, int end)
Scroll a text component inside a viewport such that the text at the specified position becomes visible.
try {
    Rectangle r1 = textPane.modelToView(start);
    Rectangle r2 = textPane.modelToView(end - 1);
    if (r1 != null) {
        if (r2 != null) {
            r1 = r1.createUnion(r2).getBounds();
        if (!textPane.getVisibleRect().contains(r1)) {
...
KeyListenermaxLength(JTextComponent textComponent, int length)
max Length
return new KeyAdapter() {
    @Override
    public void keyTyped(KeyEvent e) {
        if (textComponent.getText().length() >= length) {
            e.consume();
            textComponent.setText(textComponent.getText().substring(0, length));
};
voidpaintCurrentLineBackground(final Graphics g, final JTextComponent c, final Color col)
Paint current line background area with #getLineHighlighterBackgroundColor() .
if (c.getSelectionStart() != c.getSelectionEnd()) {
    return;
Rectangle r;
try {
    r = c.modelToView(c.getCaretPosition());
} catch (BadLocationException couldNotHappen) {
    throw new RuntimeException(couldNotHappen);
...
voidpaintUnderline(Graphics g, JTextComponent tc, int pos0, int pos1)
paint Underline
g.setColor(Color.RED);
TextUI ui = tc.getUI();
Rectangle r = ui.modelToView(tc, pos1 + 1);
int h = r.y + r.height - 2;
int max = r.x - 2;
int current = ui.modelToView(tc, pos0).x;
while (current <= max) {
    g.drawLine(current, h, Math.min(current + 2, max), h + 1);
...
DoubleparseDouble(JTextComponent textComponent)
parse Double
String text = textComponent.getText();
try {
    return Double.parseDouble(text);
} catch (Exception e) {
    return 0.0;