java - JEdi​​torPane 是最后一行

标签 java swing jeditorpane end-of-line

使用 JEditorPane,有什么方法可以判断插入符当前是否在最后一行?我在 API 中找不到任何内容,更重要的是,在派生 JEditorPane 的 JTextComponent 中找不到任何内容。当用户使用向下箭头键向下移动文本时,我需要找出这一点。我目前的想法是这样的:

private boolean isEndOfText() {
  int tmpCurrent = editor.getCaretPosition();

  editor.getActionMap().get(DefaultEditorKit.endLineAction).actionPerformed(null);
  int tmpEnd = editor.getCaretPosition();
  try { editor.setCaretPosition(tmpEnd + 1); } catch (Exception e) { editor.setCaretPosition(tmpCurrent); return true; }
  editor.setCaretPosition(tmpCurrent);
  return false;
}

只要按下向下键,此代码就会运行,并通过检测插入符号放在最后一个可能位置(即结尾)之后是否发生错误来返回是否确实是文本结尾该行的位置(如果它实际上是最后一行),否则意味着尚未到达文本末尾。

最佳答案

您应该能够使用 Text Utilities 。一种方法返回总行数,另一种方法返回插入符号处的行。

我从来没有过多地使用过 JEditorPane,因为我不喜欢它的 HTML 支持。您还可以使用 editor.getDocument().getLength() 来确定插入符号是否位于文档末尾。这适用于仅显示文本而不是 HTML 的 JTextArea 或 JTextPane。不确定它在 JEditorPane 中如何工作。

关于java - JEdi​​torPane 是最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4128435/

相关文章:

java - SwitchPreference 默认颜色

java - jni native 方法在文件打开时卡住

java - 增加所有控件的大小和字体大小

java - 单击按钮上的 JFileChooser

java - 为什么空布局和绝对位置在 Swing 中是不好的做法?

java - 我应该为什么 JEditorPane 事件创建监听器?

Java:如何在子类中设置颜色?

Java重新做一段代码...使用线程

java - 如何将 HTMLDocument 分配到 JEditorPane 中以作为源代码进行修改?

java - HTML 触发 JTabbedPane 切换选项卡