开发者

Determining text offset of a JTextArea line when wrapping

开发者 https://www.devze.com 2023-02-14 10:27 出处:网络
I am trying to determine the bounds of a line of text in my text area.I know that I can determine the bounds for an offset, which means I need to know the offset of the start and end of the line.

I am trying to determine the bounds of a line of text in my text area. I know that I can determine the bounds for an offset, which means I need to know the offset of the start and end of the line.

So I thought I would try using the getLine* methods, but it didn't give the result I expected.

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
impor开发者_C百科t javax.swing.text.DefaultHighlighter;

public class TextAreaLineBoundsTest
{
    public static void main(String[] args) throws Exception
    {
        String string = "Lorem ipsum eum putant gubergren evertitur in, no assueverit vituperatoribus eum. Ea cibo offendit vim, est et vivendum qualisque prodesset. Vis doctus expetenda contentiones an, no ius mazim epicuri expetendis, saperet salutandi forensibus ne usu. Ex fugit alterum usu. His ignota cotidieque in, augue erroribus eam no.";
        JTextArea textArea = new JTextArea(string);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);

        String term = "qualisque";
        int termOffset = string.indexOf(term);

        int termLine = textArea.getLineOfOffset(termOffset);
        int termLineStartOffset = textArea.getLineStartOffset(termLine);
        int termLineEndOffset = textArea.getLineEndOffset(termLine);

        textArea.getHighlighter().addHighlight(termLineStartOffset, termLineEndOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW));

        JScrollPane textAreaScroll = new JScrollPane(textArea);

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(textAreaScroll, BorderLayout.CENTER);
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

What it has highlighted is the entire paragraph of text, but I just want the line which contains the term I'm searching for.


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

public class TextAreaLine
{
    public static void main(String[] args) throws Exception
    {
        String string = "Lorem ipsum eum putant gubergren evertitur in, no assueverit vituperatoribus eum. Ea cibo offendit vim, est et vivendum qualisque prodesset. Vis doctus expetenda contentiones an, no ius mazim epicuri expetendis, saperet salutandi forensibus ne usu. Ex fugit alterum usu. His ignota cotidieque in, augue erroribus eam no.";
        JTextArea textArea = new JTextArea(string);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane textAreaScroll = new JScrollPane(textArea);

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(textAreaScroll, BorderLayout.CENTER);
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        String term = "qualisque";
        int termOffset = string.indexOf(term);
        Rectangle view = textArea.modelToView(termOffset);
        int startOffset = textArea.viewToModel(new Point(0, view.y));
        int endOffset = textArea.viewToModel(new Point(textArea.getSize().width, view.y) );
        textArea.getHighlighter().addHighlight(startOffset, endOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号