Java Utililty Methods JTextComponent

List of utility methods to do JTextComponent

  1. HOME
  2. Java
  3. J
  4. JTextComponent

Description

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

Method

void addChangeListener(JTextComponent text, ChangeListener changeListener)
Installs a listener to receive notification when the text of any JTextComponent is changed.
Objects.requireNonNull(text);
Objects.requireNonNull(changeListener);
DocumentListener dl = new DocumentListener() {
 private int lastChange = 0, lastNotifiedChange = 0;
 @Override
 public void insertUpdate(DocumentEvent e) {
 changedUpdate(e);
 @Override
 public void removeUpdate(DocumentEvent e) {
 changedUpdate(e);
 @Override
 public void changedUpdate(DocumentEvent e) {
 lastChange++;
 SwingUtilities.invokeLater(() -> {
 if (lastNotifiedChange != lastChange) {
 lastNotifiedChange = lastChange;
 changeListener.stateChanged(new ChangeEvent(text));
 });
};
text.addPropertyChangeListener("document", (PropertyChangeEvent e) -> {
 Document d1 = (Document) e.getOldValue();
 Document d2 = (Document) e.getNewValue();
 if (d1 != null)
 d1.removeDocumentListener(dl);
 if (d2 != null)
 d2.addDocumentListener(dl);
 dl.changedUpdate(null);
});
Document d = text.getDocument();
if (d != null)
 d.addDocumentListener(dl);
void addJTextComponentListeners(JTextComponent c, Object... objs)
add J Text Component Listeners
if (c == null)
 return;
addJContainerListeners(c, objs);
CaretListener caretListener = search(objs, CaretListener.class);
InputMethodListener inputMethodListener = search(objs, InputMethodListener.class);
if (caretListener != null)
 c.addCaretListener(caretListener);
if (inputMethodListener != null)
...
void addTextListener(JTextComponent c, Runnable listener)
add Text Listener
DocumentListener docListener = new DocumentListener() {
 public void insertUpdate(DocumentEvent ev) {
 listener.run();
 public void removeUpdate(DocumentEvent ev) {
 listener.run();
 public void changedUpdate(DocumentEvent ev) {
...
void addTextUpdateListener(JTextComponent textComponent, Consumer listener)
add Text Update Listener
Document doc = textComponent.getDocument();
doc.addDocumentListener(new DocumentListener() {
 @Override
 public void removeUpdate(DocumentEvent e) {
 listener.accept(e);
 @Override
 public void insertUpdate(DocumentEvent e) {
...
void addUndoRedo(JTextComponent comp)
add Undo Redo
final UndoManager undo = new UndoManager();
Document doc = comp.getDocument();
doc.addUndoableEditListener(new UndoableEditListener() {
 public void undoableEditHappened(UndoableEditEvent evt) {
 undo.addEdit(evt.getEdit());
});
ActionMap actionMap = comp.getActionMap();
...
int adjustForLineComment(JTextComponent editor, int iStart)
adjust For Line Comment
Document doc = editor.getDocument();
Element root = doc.getDefaultRootElement();
Element line = root.getElement(root.getElementIndex(iStart));
int iLineOffset = line.getStartOffset();
int iLength = iStart - iLineOffset;
if (iLength <= 0) {
 return iStart;
String strLine = doc.getText(iLineOffset, iLength);
if (strLine.contains("//")) {
 return iLineOffset;
return iStart;
void appendToText(JTextComponent textComp, String s)
Appends a String to the end of the document of the JTextComponent
Document doc = textComp.getDocument();
try {
 doc.insertString(doc.getLength(), s, null);
 textComp.scrollRectToVisible(textComp.modelToView(doc.getLength()));
} catch (BadLocationException e) {
 e.printStackTrace();
void applyDefaultProperties(final JTextComponent comp)
Sets default background and foreground color as well as a default font for the specified component.
if (comp == null) {
 return;
applyProperties(comp, "text", 
 "textText", 
 null); 
Font biggestFont(final javax.swing.text.JTextComponent c)
Gets the biggest font that will fit in the text component.
Font labelFont = c.getFont();
String labelText = c.getText();
int stringWidth = c.getFontMetrics(labelFont).stringWidth(labelText);
int componentWidth = c.getWidth();
double widthRatio = (double) componentWidth / (double) stringWidth;
int newFontSize = (int) Math.floor(labelFont.getSize() * widthRatio);
int componentHeight = c.getHeight();
int fontSizeToUse = Math.min(newFontSize, componentHeight);
...
void clear(JTextComponent... fields)
clear
for (JTextComponent field : fields) {
 field.setText("");


AltStyle によって変換されたページ (->オリジナル) /