use of javax.swing.text.Caret in project java-swing-tips by aterai.
the class TextAreaHandler method fire.
private void fire(Object c) {
if (c instanceof JTextComponent) {
JTextComponent tc = (JTextComponent) c;
Caret caret = tc.getCaret();
int d = caret.getDot();
int m = caret.getMark();
// LOGGER.info(() -> String.format("%s / %s", m, d));
if (d != m && (dot != d || mark != m)) {
Optional.ofNullable(tc.getSelectedText()).ifPresent(str -> {
LOGGER.info(() -> str);
// StringSelection data = new StringSelection(str);
// Toolkit.getDefaultToolkit().getSystemClipboard().setContents(data, data);
tc.copy();
});
}
dot = d;
mark = m;
}
}
use of javax.swing.text.Caret in project java-swing-tips by aterai.
the class LineCursorTextArea method updateUI.
// public LineCursorTextArea(String text, int rows, int columns) {
// super(text, rows, columns);
// }
@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);
}
use of javax.swing.text.Caret in project java-swing-tips by aterai.
the class LineCursorTextArea method paintComponent.
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Caret c = getCaret();
if (c instanceof DefaultCaret) {
Graphics2D g2 = (Graphics2D) g.create();
Rectangle r = SwingUtilities.calculateInnerArea(this, null);
DefaultCaret caret = (DefaultCaret) c;
int y = caret.y + caret.height - 1;
g2.setPaint(LINE_COLOR);
g2.drawLine(r.x, y, (int) r.getMaxX(), y);
g2.dispose();
}
}
use of javax.swing.text.Caret in project java-swing-tips by aterai.
the class HighlightCursorTextArea method updateUI.
@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);
}
use of javax.swing.text.Caret in project java-swing-tips by aterai.
the class HighlightCursorTextArea method paintComponent.
@Override
protected void paintComponent(Graphics g) {
Caret c = getCaret();
if (c instanceof DefaultCaret) {
Graphics2D g2 = (Graphics2D) g.create();
DefaultCaret caret = (DefaultCaret) c;
Rectangle r = SwingUtilities.calculateInnerArea(this, rect);
r.y = caret.y;
r.height = caret.height;
g2.setPaint(LINE_COLOR);
g2.fill(r);
g2.dispose();
}
super.paintComponent(g);
}
Aggregations