JTextArea-双击/三次单击+移动鼠标时的选择行为

时间:2019-11-04 09:43:43

标签: java swing selection jtextarea double-click

问题:当您双击JTextArea中的单词时,它会被标记,但是当您不释放鼠标按钮并尝试标记下一个单词时,它不会标记整个单词,而是而是单个字符。

在移动鼠标(双击)时,应标记整个单词(不是单个字符)。从字面上看,这是我尝试过的所有程序中的默认行为,例如:记事本,Firefox,Chrome,Word,甚至是Netbeans等。

与三次单击相同(按住并移动鼠标时应标记下一行,而不是字符)。

有什么想法吗?我在谷歌上搜索时很难,但是由于这是很平常的事情,我相信必须有一个简单的选择,或者至少有人已经有了解决方案。

示例代码:

0015      112.34
0020     4000.00
0034     9345.89
0069      723.50
0085     1500.00
0091     8237.31
0128       29.75
0129     -483.14
0135     2908.83
0189    10045.90
0251     5700.10

1 个答案:

答案 0 :(得分:0)

也许您需要创建自定义的Caret,例如:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TestJTextArea2 {
  public Component makeUI() {
    String text = "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);
      }
    };
    JPanel p = new JPanel(new GridLayout(2, 1));
    p.add(new JScrollPane(textArea1));
    p.add(new JScrollPane(textArea2));
    return p;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new TestJTextArea2().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

class SelectWordCaret extends DefaultCaret {
  private boolean wordSelectingMode = false;
  private int p0; // = Math.min(getDot(), getMark());
  private int p1; // = Math.max(getDot(), getMark());

  @Override public void mousePressed(MouseEvent e) {
    super.mousePressed(e);
    int nclicks = e.getClickCount();
    if (SwingUtilities.isLeftMouseButton(e) && !e.isConsumed() && nclicks == 2) {
      p0 = Math.min(getDot(), getMark());
      p1 = Math.max(getDot(), getMark());
      wordSelectingMode = true;
    } else {
      wordSelectingMode = false;
    }
  }

  @Override public void mouseDragged(MouseEvent e) {
    if (wordSelectingMode && !e.isConsumed() && SwingUtilities.isLeftMouseButton(e)) {
      continuouslySelectWords(e);
    } else {
      super.mouseDragged(e);
    }
  }

  private void continuouslySelectWords(MouseEvent e) {
    Position.Bias[] biasRet = new Position.Bias[1];
    JTextComponent c = getComponent();
    int pos = c.getUI().viewToModel(c, e.getPoint(), biasRet);
    if(biasRet[0] == null) {
      biasRet[0] = Position.Bias.Forward;
    }
    try {
      if (p0 < pos && pos < p1) {
        setDot(p0);
        moveDot(p1, biasRet[0]);
      } else if (p1 < pos) {
        setDot(p0);
        moveDot(Utilities.getWordEnd(c, pos), biasRet[0]);
      } else if (p0 > pos) {
        setDot(p1);
        moveDot(Utilities.getWordStart(c, pos), biasRet[0]);
      }
    } catch (BadLocationException bl) {
      UIManager.getLookAndFeel().provideErrorFeedback(c);
    }
  }
}