Java Swing JTextArea Line number

99封情书 提交于 2019-12-11 17:42:20

问题


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
keys not work unless I click on 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....


回答1:


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:

  1. wrapped text
  2. text with different size fonts (when using a JTextPane)



回答2:


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.



来源:https://stackoverflow.com/questions/52179097/java-swing-jtextarea-line-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!