Example usage for javax.swing JTextArea getLineCount

List of usage examples for javax.swing JTextArea getLineCount

Introduction

In this page you can find the example usage for javax.swing JTextArea getLineCount.

Prototype

@BeanProperty(bound = false)
public int getLineCount() 

Source Link

Document

Determines the number of lines contained in the area.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);//from www .java  2  s.c om
    ta.setWrapStyleWord(true);
    JScrollPane scroll = new JScrollPane(ta);

    ta.append("www.\n");
    ta.append("java2s\n");
    ta.append(".com");

    try {
        for (int n = 0; n < ta.getLineCount(); n += 1)
            System.out.println("line " + n + " starts at " + ta.getLineStartOffset(n) + ", ends at "
                    + ta.getLineEndOffset(n));
        System.out.println();

        int n = 0;
        while (true) {
            System.out.print("offset " + n + " is on ");
            System.out.println("line " + ta.getLineOfOffset(n));
            n += 1;
        }
    } catch (BadLocationException ex) {
        System.out.println(ex);
    }

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scroll, java.awt.BorderLayout.CENTER);
    f.setSize(150, 150);
    f.setVisible(true);
}

From source file:OffsetTest.java

public static void main(String[] args) {
    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);/*w ww . jav a 2  s. c o m*/
    ta.setWrapStyleWord(true);
    JScrollPane scroll = new JScrollPane(ta);

    // Add three lines of text to the JTextArea.
    ta.append("The first line.\n");
    ta.append("Line Two!\n");
    ta.append("This is the 3rd line of this document.");

    // Print some results . . .
    try {
        for (int n = 0; n < ta.getLineCount(); n += 1)
            System.out.println("line " + n + " starts at " + ta.getLineStartOffset(n) + ", ends at "
                    + ta.getLineEndOffset(n));
        System.out.println();

        int n = 0;
        while (true) {
            System.out.print("offset " + n + " is on ");
            System.out.println("line " + ta.getLineOfOffset(n));
            n += 13;
        }
    } catch (BadLocationException ex) {
        System.out.println(ex);
    }

    // Layout . . .
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scroll, java.awt.BorderLayout.CENTER);
    f.setSize(150, 150);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel topPanel = new JPanel();
    topPanel.setPreferredSize(new Dimension(200, 200));
    topPanel.setBackground(Color.WHITE);

    JTextArea chatArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(chatArea);

    JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
    mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    mainPanel.add(topPanel, BorderLayout.CENTER);
    mainPanel.add(scrollPane, BorderLayout.SOUTH);

    chatArea.getDocument().addDocumentListener(new DocumentListener() {

        @Override/* w  w w . j  av  a  2s . c o m*/
        public void insertUpdate(DocumentEvent e) {
            updateLineCount();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            updateLineCount();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            updateLineCount();
        }

        private void updateLineCount() {
            int lineCount = chatArea.getLineCount();
            if (lineCount <= 4) {
                chatArea.setRows(lineCount);
                mainPanel.revalidate();
            }
        }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(mainPanel);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static int getTextableHeight(JTextArea textArea) {
    int textableHeight = textArea.getLineCount() * textArea.getFontMetrics(textArea.getFont()).getHeight();
    Insets insets = textArea.getInsets();
    if (insets != null)
        textableHeight += insets.top + insets.bottom;

    return textableHeight;
}

From source file:com.prodigy4440.view.MainJFrame.java

public static String[] getTextLineByLine(JTextArea area) {
    int numberOfLine = area.getLineCount();
    String resultString[] = new String[numberOfLine];
    String text = area.getText();
    for (int i = 0; i < numberOfLine; i++) {
        try {// w ww . j  av a 2s . com
            int start = area.getLineStartOffset(i);
            int end = area.getLineEndOffset(i);

            String line = text.substring(start, end);
            resultString[i] = line;
        } catch (BadLocationException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return resultString;
}

From source file:testFileHandler.java

public void writeFile(String path, javax.swing.JTextArea textField) throws IOException {

    FileWriter fw = new FileWriter(path); //create a file writer to write to the path
    try (PrintWriter pw = new PrintWriter(fw)) {

        String s[] = textField.getText().split("\\r?\\n"); //get the text fileds characters line by line and put them into an arry so that we can write those text fields as it is.
        ArrayList<String> arrList = new ArrayList<>(Arrays.asList(s));
        for (int i = 0; i < textField.getLineCount(); i++) {
            pw.println(arrList.get(i)); //get the string in that array one by one and write them into the file that opened.
        }/*from   w  w  w . j  a  va2 s  . c  o m*/
        pw.close(); // close the file when writting is done.
    } catch (Exception e) {
        System.out.println(e);
    }

}