如何将换行考虑到JTextArea行计数? [英] How to take line wrap in account for JTextArea line counting?

查看:66
本文介绍了如何将换行考虑到JTextArea行计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道此的粘贴.

int rowStartOffset = textComponent.viewToModel(new Point(0, 0));
int endOffset = textComponent.viewToModel(new Point(0, textComponent.getHeight()));

int count = 0; //used to store the line count
while (rowStartOffset < endOffset) {
    try {
        rowStartOffset = Utilities.getRowEnd(textComponent, rowStartOffset) + 1;
    } catch (BadLocationException ex) {
        break;
    }
    count++;
}

在启用/未启用换行的情况下,我进行了几次测试,而且似乎可以正常工作.

解决方案

在我看来,只要您没有在Text Area上调用setPreferredSizegetPreferredSize方法就应该为您提供确切的信息正在寻找麻烦...

这就是为什么您不应该调用setPreferredSize来做布局工作的原因.首选大小应该由组件计算(例如,在设置文本时).

让我知道我是否遗漏了要点;)

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CalculateTextAreaSize extends Box{

    public CalculateTextAreaSize(){
        super(BoxLayout.Y_AXIS);

        final JTextArea text = new JTextArea("I've\nGot\nA\nLovely\nBunch\nof\nCoconuts!\n");

        JScrollPane pane = new JScrollPane(text);

        add(pane);

        JButton button = new JButton("Set Text!");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                text.insert("Longish string - how long will it be?", text.getDocument().getLength());
                //The size it will be once everything is figured out
                System.out.println(text.getPreferredSize());
                //The size it is now because no rendering has been done
                System.out.println(text.getSize());
            }
        });
        add(button);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new CalculateTextAreaSize());
        frame.validate();
        frame.pack();
        frame.setVisible(true);
    }

}

I'm aware of this thread showing a way to do but it leads to strange behaviors in my case and i'm wondering if there is no simpler way to do it.

I need to know the size my JTextArea will have after setting the text. Here is how i do at the moment:

tarea.getLineCount() * tarea.getRowHeight();

It works unless there is no line wrapping. I'd like to do the same calculation with line wrapping. Is there any to know when a line wrapping happen? This way i would only need to increment the current line count by one.

EDIT:

Here is (maybe) a solution i found. It's almost copy paste of this by @camickr.

int rowStartOffset = textComponent.viewToModel(new Point(0, 0));
int endOffset = textComponent.viewToModel(new Point(0, textComponent.getHeight()));

int count = 0; //used to store the line count
while (rowStartOffset < endOffset) {
    try {
        rowStartOffset = Utilities.getRowEnd(textComponent, rowStartOffset) + 1;
    } catch (BadLocationException ex) {
        break;
    }
    count++;
}

I made few tests with/without line wrapping enabled and it seemed to work.

解决方案

It seems to me that so long as you haven't done called setPreferredSize on the Text Area, the getPreferredSize method should give you exactly what you're looking for without the hassle...

This is the reason why you shouldn't call setPreferredSize to do layout stuff. Preferred size is supposed to be calculated by the component (when text is set for example).

Let me know if I'm missing the point ;)

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class CalculateTextAreaSize extends Box{

    public CalculateTextAreaSize(){
        super(BoxLayout.Y_AXIS);

        final JTextArea text = new JTextArea("I've\nGot\nA\nLovely\nBunch\nof\nCoconuts!\n");

        JScrollPane pane = new JScrollPane(text);

        add(pane);

        JButton button = new JButton("Set Text!");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                text.insert("Longish string - how long will it be?", text.getDocument().getLength());
                //The size it will be once everything is figured out
                System.out.println(text.getPreferredSize());
                //The size it is now because no rendering has been done
                System.out.println(text.getSize());
            }
        });
        add(button);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new CalculateTextAreaSize());
        frame.validate();
        frame.pack();
        frame.setVisible(true);
    }

}

这篇关于如何将换行考虑到JTextArea行计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆