Java Code Examples for javax.swing.text.Caret#setBlinkRate()

The following examples show how to use javax.swing.text.Caret#setBlinkRate() . 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: 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 2
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static Component makeTextArea(boolean flag) {
  JTextArea textArea = new JTextArea() {
    @Override public void updateUI() {
      setCaret(null);
      super.updateUI();
      if (flag) {
        Caret oldCaret = getCaret();
        int blinkRate = oldCaret.getBlinkRate();
        // int blinkRate = UIManager.getInt("TextField.caretBlinkRate")
        Caret caret = new FocusCaret();
        caret.setBlinkRate(blinkRate);
        setCaret(caret);
        caret.setSelectionVisible(true);
      }
    }
  };
  textArea.setText("aaa\nbbbbbb\ncccccccccccc\n");
  textArea.selectAll();
  return new JScrollPane(textArea);
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private MainPanel() {
  super(new GridLayout(2, 1));
  String text = String.join("\n", Collections.nCopies(3, "The quick brown fox jumps over the lazy dog."));

  JTextArea textArea1 = new JTextArea("default\n" + text);
  JTextArea textArea2 = new JTextArea("setCaret\n" + text) {
    @Override public void updateUI() {
      setCaret(null);
      super.updateUI();
      Caret oldCaret = getCaret();
      int blinkRate = oldCaret.getBlinkRate();
      Caret caret = new SelectWordCaret();
      caret.setBlinkRate(blinkRate);
      setCaret(caret);
    }
  };
  add(new JScrollPane(textArea1));
  add(new JScrollPane(textArea2));
  setPreferredSize(new Dimension(320, 240));
}
 
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: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  JTextField field1 = new JTextField("0987654321");
  field1.setSelectedTextColor(Color.RED);
  field1.setSelectionColor(Color.GREEN);

  HighlightPainter selectionPainter = new DefaultHighlightPainter(Color.WHITE) {
    @Override public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) {
      Shape s = super.paintLayer(g, offs0, offs1, bounds, c, view);
      if (s instanceof Rectangle) {
        Rectangle r = (Rectangle) s;
        g.setColor(Color.ORANGE);
        g.fillRect(r.x, r.y + r.height - 2, r.width, 2);
      }
      return s;
    }
  };
  Caret caret = new DefaultCaret() {
    @Override protected HighlightPainter getSelectionPainter() {
      return selectionPainter;
    }
  };
  JTextField field2 = new JTextField("123465789735");
  caret.setBlinkRate(field2.getCaret().getBlinkRate());
  field2.setSelectedTextColor(Color.RED);
  field2.setCaret(caret);

  Box box = Box.createVerticalBox();
  box.add(makeTitledPanel("Default", new JTextField("12345")));
  box.add(Box.createVerticalStrut(10));
  box.add(makeTitledPanel("JTextComponent#setSelectionColor(...)", field1));
  box.add(Box.createVerticalStrut(10));
  box.add(makeTitledPanel("JTextComponent#setCaret(...)", field2));
  box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  add(box, BorderLayout.NORTH);
  setPreferredSize(new Dimension(320, 240));
}