【问题标题】:getting the changed lines jtextarea获取更改的行 jtextarea
【发布时间】:2012-02-09 00:41:54
【问题描述】:

我正在做一个需要自定义验证的文本编辑器。由于内容非常大,我想只验证更改或添加的行。验证错误由行号显示,例如“行:10 不能超过 15 个字符”

  1. 对于单行,每次用户更改时,我都会验证当前行,并保留行号作为参考。 - 解决了

  2. 用户可以复制文本并粘贴 - 多行。为此,想到了 getSelectionStart 和 getSelectionEnd。有没有办法从 getSelectionStart 和 getSelectionEnd 获取行号,所以我可以得到起始行和结束行?

经过一些实验,我认为选择可见的线条将解决我上面提到的第二个问题。

矩形将解决获取可视区域的 x、y 坐标并编写代码,我想我快完成了。但是,我没有正确获得结束行号,

      [code]
    //editor is jtextarea
      Rectangle r = editor.getVisibleRect();
      Point top = new Point(r.x, r.y);
      Point bottom = new Point(r.x, r.y + r.height);
      int startRow = editor.viewToModel(top); /* this is working. it shows 0 at           initial, then after the line reaches the end and when the scrollbar gets displayed, it shows the numbers correctly, 1,2,3...*/
     int endRow = editor.viewToModel(bottom); /* this is not working, when we type, it is taking column numbers */
     editorLineNo.setText(" START ROW " + startRow + " END ROW" + endRow);
     [/code]

     What is needed is, start row number and end row number from the viewable area  of jtextarea  

【问题讨论】:

  • 感谢您的回答,这非常有用,现在我想,只选择可见的代码。为此,我使用 Rectangle 来获取可见部分。剩下的唯一部分是将矩形 y 坐标转换为行,这里是代码,pastebin.com/AvCWqNzj
  • 谢谢,camickr。我快到了,只有最后的行号没有正确显示,pastebin.com/Ug​​KCHgcx – shiva0101 m

标签: java swing jtextarea


【解决方案1】:

有没有办法获取行号...

Element root = textArea.getDocument().getDefaultRootElement();
int row = root.getElementIndex( selectionStart ) + 1;

【讨论】:

  • 感谢您的回答,这非常有用,现在我想,只选择可见的代码。为此,我使用 Rectangle 来获取可见部分。剩下的唯一部分是将矩形 y 坐标转换为行,这里是代码,[链接]pastebin.com/vU1KZCwK
  • 您可以使用viewToModel(...)方法获取并偏移到Document中,然后使用上面的代码获取行。
  • 谢谢,camickr。我快到了,只有最后的行号没有正确,pastebin.com/UgKCHgcx
  • 发布您的SSCCE 以展示您对原始问题的问题。
【解决方案2】:

【讨论】:

  • 这没有给出“行号”。
  • 感谢您的回答,这非常有用,现在我想,只选择可见的代码。为此,我使用 Rectangle 来获取可见部分。剩下的唯一部分是将矩形 y 坐标转换为行,这里是代码,[链接]pastebin.com/vU1KZCwK
  • viewToModel() 传递 x、y 并获取文档中的位置。
猜你喜欢
  • 1970-01-01
  • 2013-07-19
  • 2011-11-11
  • 1970-01-01
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多