List of usage examples for javax.swing JTextArea getLineStartOffset
public int getLineStartOffset(int line) throws BadLocationException
From source file:MainClass.java
public static void main(String[] args) { JTextArea ta = new JTextArea(); ta.setLineWrap(true);//from w w w.ja v a2 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);/*from ww w . j av 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: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 ww w . j av a2s . 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; }
From source file:com.monead.semantic.workbench.SemanticWorkbench.java
/** * Creates the status message for the error, alerts the user with a popup. If * the issue is a recognized syntax error and the line and column numbers can * be found int he exception message, the cursor will be moved to that * position./*from w w w .j av a2 s . c om*/ * * @param throwable * The error that occurred * @param operationDetailMessage * A message specific to the operation that was running. This may be * null * * @return The message to be presented on the status line */ private String errorAlert(Throwable throwable, String operationDetailMessage) { String statusMessage; String alertMessage; String httpStatusMessage = null; String causeClass; String causeMessage; QueryExceptionHTTP httpExc = null; int[] lineAndColumn = new int[2]; int whichSelectedTab = -1; JTextArea whichFocusJTextArea = null; if (throwable.getCause() != null) { causeClass = throwable.getCause().getClass().getName(); causeMessage = throwable.getCause().getMessage(); } else { causeClass = throwable.getClass().getName(); causeMessage = throwable.getMessage(); } if (operationDetailMessage != null) { alertMessage = operationDetailMessage + "\n\n"; } else { alertMessage = "Error:"; } alertMessage += causeClass + "\n" + causeMessage; statusMessage = causeMessage.trim().length() > 0 ? causeMessage : causeClass; if (throwable instanceof QueryExceptionHTTP) { httpExc = (QueryExceptionHTTP) throwable; httpStatusMessage = httpExc.getResponseMessage(); if (httpStatusMessage == null || httpStatusMessage.trim().length() == 0) { try { httpStatusMessage = HttpStatus.getStatusText(httpExc.getResponseCode()); } catch (Throwable lookupExc) { LOGGER.info("Cannot find message for returned HTTP code of " + httpExc.getResponseCode()); } } } if (httpExc != null) { statusMessage += ": " + "Response Code: " + httpExc.getResponseCode() + (httpStatusMessage != null && httpStatusMessage.trim().length() > 0 ? " (" + httpStatusMessage + ")" : ""); JOptionPane.showMessageDialog(this, alertMessage + "\n\n" + "Response Code: " + httpExc.getResponseCode() + "\n" + (httpStatusMessage != null ? httpStatusMessage : ""), "Error", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog(this, alertMessage, "Error", JOptionPane.ERROR_MESSAGE); } // Attempt to deal with a syntax error and positioning the cursor if (runningOperation == Operation.CREATE_MODEL) { // Assertions processing issue RiotException riotExc = null; Throwable nextThrowable = throwable; while (riotExc == null && nextThrowable != null) { if (nextThrowable instanceof RiotException) { riotExc = (RiotException) nextThrowable; } else { LOGGER.trace("Not a riot exception, another? " + throwable.getClass().toString() + "->" + throwable.getCause()); nextThrowable = nextThrowable.getCause(); } } if (riotExc != null) { lineAndColumn = getSyntaxErrorLineColLocation(riotExc.getMessage().toLowerCase(), "line: ", ",", "col: ", "]"); whichSelectedTab = TAB_NUMBER_ASSERTIONS; whichFocusJTextArea = assertionsInput; } else { LOGGER.debug("No riot exception found so the caret cannot be positioned"); } } if (runningOperation == Operation.EXECUTE_SPARQL) { // SPARQL processing issue QueryParseException queryParseExc = null; Throwable nextThrowable = throwable; while (queryParseExc == null && nextThrowable != null) { if (nextThrowable instanceof QueryParseException) { queryParseExc = (QueryParseException) nextThrowable; } else { LOGGER.trace("Not a query parse exception, another? " + throwable.getClass().toString() + "->" + throwable.getCause()); nextThrowable = nextThrowable.getCause(); } } if (queryParseExc != null) { lineAndColumn = getSyntaxErrorLineColLocation(queryParseExc.getMessage().toLowerCase(), "at line ", ",", ", column ", "."); whichSelectedTab = TAB_NUMBER_SPARQL; whichFocusJTextArea = sparqlInput; } else { LOGGER.debug("No query parse exception found so the caret cannot be positioned"); } } if (lineAndColumn[0] > 0 && lineAndColumn[1] > 0) { LOGGER.debug("Attempt to set assertions caret to position (" + lineAndColumn[0] + "," + lineAndColumn[1] + ")"); final int finalLineNumber = lineAndColumn[0] - 1; final int finalColumnNumber = lineAndColumn[1] - 1; final int finalWhichSelectedTab = whichSelectedTab; final JTextArea finalWhichFocusJTextArea = whichFocusJTextArea; SwingUtilities.invokeLater(new Runnable() { public void run() { tabbedPane.setSelectedIndex(finalWhichSelectedTab); try { finalWhichFocusJTextArea.setCaretPosition( finalWhichFocusJTextArea.getLineStartOffset(finalLineNumber) + finalColumnNumber); finalWhichFocusJTextArea.requestFocusInWindow(); } catch (Throwable throwable) { LOGGER.warn("Cannot set " + finalWhichFocusJTextArea.getName() + " carat position to (" + finalLineNumber + "," + finalColumnNumber + ") on tab " + finalWhichSelectedTab, throwable); } } }); } return statusMessage; }