List of usage examples for javax.swing JTextArea getLineEndOffset
public int getLineEndOffset(int line) throws BadLocationException
From source file:MainClass.java
public static void main(String[] args) { JTextArea ta = new JTextArea(); ta.setLineWrap(true);//from ww w .ja v a 2 s . com 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);/*from w ww.ja va 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: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 {/*from w w w. j av a2 s . c o m*/ 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; }