简体   繁体   English

Java Swing JTextArea行号

[英]Java Swing JTextArea Line number

This count line number of textarea. 此计数文本区域的行数。 The code works correctly, but when run this code the textarea is not active, the caret is hidden and the keyboard 该代码正常工作,但是在运行该代码时,textarea处于不活动状态,插入符号被隐藏,键盘
keys not work unless I click on textarea. 除非我单击textarea,否则键将不起作用。

code: 码:

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

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Element;

public class LineNumber extends JFrame implements DocumentListener {

    private static final long serialVersionUID = -1093726028044203117L;

    private JScrollPane scroll;
    private JTextArea textArea, lineArea;

    public static void main(String[] args) {

        new LineNumber().setVisible(true);

    }

    public LineNumber() {

        super("Line Numbers");

        setSize(500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setUI();
    }

    private void setUI() {

        textArea = new JTextArea();

        lineArea = new JTextArea(0, 3);
        lineArea.setEditable(false);
        lineArea.setForeground(Color.GRAY);

        scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        textArea.getDocument().addDocumentListener(this);

        scroll.setViewportView(textArea);
        scroll.setRowHeaderView(lineArea);
        getContentPane().add(scroll, BorderLayout.CENTER);

    }

    public void changedUpdate(DocumentEvent event) {

        lineArea.setFont(textArea.getFont());
        lineArea.setText(getLine());

    }

    public void insertUpdate(DocumentEvent event) {

        lineArea.setFont(textArea.getFont());
        lineArea.setText(getLine());
    }

    public void removeUpdate(DocumentEvent event) {

        lineArea.setFont(textArea.getFont());
        lineArea.setText(getLine());
    }

    public String getLine() {

        int caretPos = 0;
        String lines;

        caretPos = textArea.getDocument().getLength();
        Element root = textArea.getDocument().getDefaultRootElement();
        lines = String.format("%s%s", 1, System.lineSeparator());

        for (int i = 2; i < root.getElementIndex(caretPos) + 2; i++) {
            lines += String.format("%s%s", i, System.lineSeparator());

        }

        return lines;

    }

}

If I do not add lineArea to the scrollpane the textarea work correctly but after add to setRowHeaderView the textarea only gets active with a mouse click.... 如果我没有将lineArea添加到滚动窗格中,textarea可以正常工作,但是在添加到setRowHeaderView之后,textarea仅通过单击鼠标即可激活。

By default it focus on first component, so if you want to focus on another one try this code in the constructor. 默认情况下,它专注于第一个组件,因此,如果要关注另一个组件,请尝试在构造函数中使用此代码。

addWindowFocusListener(new WindowAdapter() {
    @Override
    public void windowGainedFocus(WindowEvent e) {
        textArea.requestFocusInWindow();
    }
});

textArea is now focused, more on this . 现在, textArea 更加专注于此

You can prevent the line number text area from gaining focus by using: 您可以使用以下方法防止行号文本区域获得焦点:

lineArea = new JTextArea(0, 3);
lineArea.setEditable(false);
lineArea.setFocusable(false);

You can also check out Text Component Line Number for a fancier implementation that supports: 您还可以签出Text Component Line Number文本组件行号)以获得支持以下功能的更高级实现:

  1. wrapped text 包装文字
  2. text with different size fonts (when using a JTextPane) 具有不同大小字体的文本(使用JTextPane时)

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

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