简体   繁体   English

Java JTextArea行号

[英]Java JTextArea Line Numbers

I'm trying to add line numbers to a JTextArea and am having some difficulties. 我试图将行号添加到JTextArea并遇到一些困难。 The line numbers appear, but they do not scroll properly. 行号出现,但是它们不能正确滚动。

I have a linked-list of a custom class that stores a line of data (log message) and a line number associated with it as well as visibility on whether it should be shown in the text area or not. 我有一个自定义类的链表,该链表存储一行数据(日志消息)和与之关联的行号,以及是否应在文本区域中显示它的可见性。 So what I did was create two JTextAreas... one to store the logs, and the other to store the line numbers. 因此,我要做的是创建两个JTextAreas ...一个用于存储日志,另一个用于存储行号。

The layout works and the line numbers populate correctly with the logs. 布局有效,并且行号已正确填充日志。 The problem is when I try to scroll up or down. 问题是当我尝试向上或向下滚动时。 The logs adjust properly as you scroll, but the line numbers do not. 滚动时日志会正确调整,但行号却不会。 Nothing shows beyond the initial 28 line numbers that are shown initially. 除了最初显示的最初28行编号之外,没有任何显示。 The space is just blank. 该空间只是空白。

My code is below: 我的代码如下:

public class CustomLineNumbers extends JFrame implements ActionListener
{    
    ...
    private JTextArea logField;
    private JTextArea lineField;

    private List<Log> logs;

    public CustomLineNumbers() 
    {       
        ...
        logs = new ArrayList<Log>();

        logField = new JTextArea(28, 68);
        logField.setMargin(new Insets(0, 5, 0, 0));
        logField.setEditable(false);
        logField.setLineWrap(true);
        logField.setWrapStyleWord(true);

        lineField = new JTextArea();
        lineField.setPreferredSize(new Dimension(25, 0));
        lineField.setBackground(this.getForeground());
        lineField.setBorder(OUTER);
        lineField.setEditable(false);

        initLogs();
        updateLogView();
        updateLineView();

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.getViewport().add(logField);
        scrollPane.setRowHeaderView(lineField);
        scrollPane.setVertical...Policy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        ...
    }

    private void initLogs()
    {
        // inits the data in the list
    }

    public void updateLogView()
    {
        logField.setText("");   // reset log field to nothing

        for (int i = 0; i < logs.size(); i++)
        {
            // Append only if the line is visible
            if (logs.get(i).getVisibility())
            {
                // if this isn't the first line, 
                // add a line break before appending
                if (i > 0)
                    logField.append("\n");  

                logField.append(logs.get(i).getLine());
            }
        }       
    }

    public void updateLineView()
    {
        lineField.setText("");  // reset log field to nothing

        for (int i = 0; i < logs.size(); i++)
        {
            // Append only if the line is visible
            if (logs.get(i).getVisibility())
            {
                // if this isn't the first line, 
                // add a line break before appending
                if (i > 0)
                    lineField.append("\n"); 

                lineField.append("" + logs.get(i).getLineNumber());
            }
        }       
    }

    ...

    /***** Main Execution *****/
    public static void main(String[] args) { ... }
}

Any ideas? 有任何想法吗?

Thanks, 谢谢,

Have you tried putting both text fields in the viewPort? 您是否尝试过将两个文本字段都放在viewPort中? Maybe on a panel that gives the line numbers a fraction of the available width? 也许在面板上,使行号为可用宽度的一小部分?

The compenent used to display the line numbers can be added using: 可以使用以下命令添加用于显示行号的组件:

scrollPane.setRowHeaderView(...);

Another option is to use a JTable to display both columns. 另一个选择是使用JTable显示两个列。 Using two text areas is really not the best solution. 使用两个文本区域确实不是最佳解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM