Java Code Examples for javax.swing.text.JTextComponent#repaint()

The following examples show how to use javax.swing.text.JTextComponent#repaint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CaretItem.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Set new caret bounds while repainting both original and new bounds.
 * Clear the caret-painted and update-caret-bounds flags.
 *
 * @param newCaretBounds non-null new bounds
 */
synchronized Rectangle setCaretBoundsWithRepaint(Rectangle newCaretBounds, JTextComponent c, String logMessage, int logIndex) {
    Rectangle oldCaretBounds = this.caretBounds;
    boolean repaintOld = (oldCaretBounds != null && (this.statusBits & CARET_PAINTED) != 0);
    this.statusBits &= ~(CARET_PAINTED | UPDATE_CARET_BOUNDS);
    boolean log = EditorCaret.LOG.isLoggable(Level.FINE);
    if (repaintOld) {
        if (log) {
            logRepaint(logMessage + "-setBoundsRepaint-repaintOld", logIndex, oldCaretBounds);
        }
        c.repaint(oldCaretBounds); // First schedule repaint of the original bounds (even if new bounds will possibly be the same)
    }
    if (!repaintOld || !newCaretBounds.equals(oldCaretBounds)) {
        if (log) {
            logRepaint(logMessage + "-setBoundsRepaint-repaintNew", logIndex, newCaretBounds);
        }
        c.repaint(newCaretBounds);
    }
    this.caretBounds = newCaretBounds;
    return oldCaretBounds;
}
 
Example 2
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void updateUI() {
  super.updateUI();
  setOpaque(false);
  Caret caret = new DefaultCaret() {
    // [UnsynchronizedOverridesSynchronized]
    // Unsynchronized method damage overrides synchronized method in DefaultCaret
    @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
    @Override protected synchronized void damage(Rectangle r) {
      if (Objects.nonNull(r)) {
        JTextComponent c = getComponent();
        x = 0;
        y = r.y;
        width = c.getSize().width;
        height = r.height;
        c.repaint();
      }
    }
  };
  // caret.setBlinkRate(getCaret().getBlinkRate());
  caret.setBlinkRate(UIManager.getInt("TextArea.caretBlinkRate"));
  setCaret(caret);
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
public static void setHighlight(JTextComponent jtc, String pattern, HighlightPainter painter) {
  Highlighter highlighter = jtc.getHighlighter();
  highlighter.removeAllHighlights();
  Document doc = jtc.getDocument();
  try {
    String text = doc.getText(0, doc.getLength());
    Matcher matcher = Pattern.compile(pattern).matcher(text);
    int pos = 0;
    while (matcher.find(pos) && !matcher.group().isEmpty()) {
      int start = matcher.start();
      int end = matcher.end();
      highlighter.addHighlight(start, end, painter);
      pos = end;
    }
  } catch (BadLocationException | PatternSyntaxException ex) {
    UIManager.getLookAndFeel().provideErrorFeedback(jtc);
  }
  jtc.repaint();
}
 
Example 4
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void updateUI() {
  super.updateUI();
  Caret caret = new DefaultCaret() {
    // [UnsynchronizedOverridesSynchronized]
    // Unsynchronized method damage overrides synchronized method in DefaultCaret
    @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
    @Override protected synchronized void damage(Rectangle r) {
      if (Objects.nonNull(r)) {
        JTextComponent c = getComponent();
        x = 0;
        y = r.y;
        width = c.getSize().width;
        height = r.height;
        c.repaint();
      }
    }
  };
  // caret.setBlinkRate(getCaret().getBlinkRate());
  caret.setBlinkRate(UIManager.getInt("TextArea.caretBlinkRate"));
  setCaret(caret);
}
 
Example 5
Source File: CurrentLineHighlighter.java    From nextreports-designer with Apache License 2.0 6 votes vote down vote up
/** 
 * Fetches the previous caret location, stores the current caret location, 
 * If the caret is on another line, repaint the previous line and the current line 
 * 
 * @param c the text component 
 */ 
private static void currentLineChanged(JTextComponent c) {
    try { 
        int previousCaret = ((Integer) c.getClientProperty(PREVIOUS_CARET)).intValue(); 
        Rectangle prev = c.modelToView(previousCaret); 
        Rectangle r = c.modelToView(c.getCaretPosition()); 
        c.putClientProperty(PREVIOUS_CARET, new Integer(c.getCaretPosition())); 
 
        if ((prev != null) && (prev.y != r.y)) { 
            c.repaint(0, prev.y, c.getWidth(), r.height); 
            c.repaint(0, r.y, c.getWidth(), r.height); 
        } 
    } catch (BadLocationException e) {
    	// ignore
    } 
}
 
Example 6
Source File: FlatTextFieldUI.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
static void propertyChange( JTextComponent c, PropertyChangeEvent e ) {
	switch( e.getPropertyName() ) {
		case FlatClientProperties.PLACEHOLDER_TEXT:
		case FlatClientProperties.COMPONENT_ROUND_RECT:
			c.repaint();
			break;

		case FlatClientProperties.MINIMUM_WIDTH:
			c.revalidate();
			break;
	}
}
 
Example 7
Source File: Utility.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Scrolls the specified text component so the text between the starting and ending index are visible.
 */
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
    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();
    }
}
 
Example 8
Source File: Utility.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Scrolls the specified text component so the text between the starting and ending index are visible.
 */
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
    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();
    }
}
 
Example 9
Source File: BaseCaret.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void updateRectangularSelectionPaintRect() {
    // Repaint current rect
    JTextComponent c = component;
    Rectangle repaintRect = rsPaintRect;
    if (rsDotRect == null || rsMarkRect == null) {
        return;
    }
    Rectangle newRect = new Rectangle();
    if (rsDotRect.x < rsMarkRect.x) { // Swap selection to left
        newRect.x = rsDotRect.x; // -1 to make the visual selection non-empty
        newRect.width = rsMarkRect.x - newRect.x;
    } else { // Extend or shrink on right
        newRect.x = rsMarkRect.x;
        newRect.width = rsDotRect.x - newRect.x;
    }
    if (rsDotRect.y < rsMarkRect.y) {
        newRect.y = rsDotRect.y;
        newRect.height = (rsMarkRect.y + rsMarkRect.height) - newRect.y;
    } else {
        newRect.y = rsMarkRect.y;
        newRect.height = (rsDotRect.y + rsDotRect.height) - newRect.y;
    }
    if (newRect.width < 2) {
        newRect.width = 2;
    }
    rsPaintRect = newRect;

    // Repaint merged region with original rect
    if (repaintRect == null) {
        repaintRect = rsPaintRect;
    } else {
        repaintRect = repaintRect.union(rsPaintRect);
    }
    c.repaint(repaintRect);
    
    updateRectangularSelectionPositionBlocks();
}
 
Example 10
Source File: CaretItem.java    From netbeans with Apache License 2.0 5 votes vote down vote up
synchronized Rectangle repaint(JTextComponent c, String logMessage, int logIndex) {
    Rectangle bounds = this.caretBounds;
    if (bounds != null) {
        this.statusBits &= ~CARET_PAINTED;
        if (EditorCaret.LOG.isLoggable(Level.FINE)) {
            logRepaint(logMessage, logIndex, bounds);
        }
        c.repaint(bounds);
    }
    return bounds;
}
 
Example 11
Source File: CaretItem.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Repaint caret bounds if the caret is showing or do nothing
 * @param c
 * @return 
 */
synchronized Rectangle repaintIfShowing(JTextComponent c, String logMessage, int logIndex) {
    Rectangle bounds = this.caretBounds;
    if (bounds != null) {
        boolean repaint = (this.statusBits & CARET_PAINTED) != 0;
        if (repaint) {
            this.statusBits &= ~CARET_PAINTED;
            if (EditorCaret.LOG.isLoggable(Level.FINE)) {
                logRepaint(logMessage + "-repaintIfShowing", logIndex, bounds);
            }
            c.repaint(bounds);
        }
    }
    return bounds;
}
 
Example 12
Source File: EditorCaret.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void updateRectangularSelectionPaintRect() {
    // Repaint current rect
    JTextComponent c = component;
    Rectangle repaintRect = rsPaintRect;
    if (rsDotRect == null || rsMarkRect == null) {
        return;
    }
    Rectangle newRect = new Rectangle();
    if (rsDotRect.x < rsMarkRect.x) { // Swap selection to left
        newRect.x = rsDotRect.x; // -1 to make the visual selection non-empty
        newRect.width = rsMarkRect.x - newRect.x;
    } else { // Extend or shrink on right
        newRect.x = rsMarkRect.x;
        newRect.width = rsDotRect.x - newRect.x;
    }
    if (rsDotRect.y < rsMarkRect.y) {
        newRect.y = rsDotRect.y;
        newRect.height = (rsMarkRect.y + rsMarkRect.height) - newRect.y;
    } else {
        newRect.y = rsMarkRect.y;
        newRect.height = (rsDotRect.y + rsDotRect.height) - newRect.y;
    }
    if (newRect.width < 2) {
        newRect.width = 2;
    }
    rsPaintRect = newRect;

    // Repaint merged region with original rect
    if (repaintRect == null) {
        repaintRect = rsPaintRect;
    } else {
        repaintRect = repaintRect.union(rsPaintRect);
    }
    c.repaint(repaintRect);
    
    updateRectangularSelectionPositionBlocks();
}
 
Example 13
Source File: Utility.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Scrolls the specified text component so the text between the starting and ending index are visible.
 */
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
    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();
    }
}
 
Example 14
Source File: Utility.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Scrolls the specified text component so the text between the starting and ending index are visible.
 */
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
    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();
    }
}
 
Example 15
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
@Override protected synchronized void damage(Rectangle r) {
  if (Objects.nonNull(r)) {
    JTextComponent c = getComponent();
    x = r.x;
    y = r.y;
    // width = c.getFontMetrics(c.getFont()).charWidth('w');
    // width = c.getFontMetrics(c.getFont()).charWidth('\u3042');
    width = c.getFontMetrics(c.getFont()).charWidth('あ');
    height = r.height;
    c.repaint();
  }
}