java - get text from JTextArea by number line

梦想的初衷 提交于 2019-12-02 01:40:55
MadProgrammer

Simply based on the example from your previous question...

Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
Element element = root.getElement(2);
int start = element.getStartOffset();
int end = element.getEndOffset();
System.out.println(doc.getText(start, end - start));

And the runnable code

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;

public class ElementEndOffsetTest {

    public JComponent makeUI() {
        String str = "name : andy\n"
                + "birth : jakarta, 1 jan 1990\n"
                + "number id : 01011990 01\n"
                + "age : 26\n"
                + "study : Informatics engineering\n";

        JTextArea textArea = new JTextArea(str);
        textArea.setEditable(false);
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JScrollPane(textArea));
        p.add(new JButton(new AbstractAction("add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Document doc = textArea.getDocument();
                    Element root = doc.getDefaultRootElement();
                    Element element = root.getElement(2);
                    int start = element.getStartOffset();
                    int end = element.getEndOffset();
                    System.out.println(doc.getText(start, end - start));
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
            }
        }), BorderLayout.SOUTH);
        return p;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.getContentPane().add(new ElementEndOffsetTest().makeUI());
            f.setSize(320, 240);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        });
    }
}

Based on your questions, I would, however, recommend you consider using a JTable instead, it would be easier, see How to Use Tables for more details

try following

String[] lines = jTextArea.getText().split("\n"); String numberId = lines[3];

or

String numberId = jTextArea.getText().split("\n")[3];

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