List of usage examples for javax.swing.text Document getLength
public int getLength();
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textComp = new JTextField("Initial Text"); Document doc = textComp.getDocument(); // Append some text doc.insertString(doc.getLength(), "some text", null); }
From source file:Main.java
public static void main(String args[]) throws BadLocationException { JFrame jf = new JFrame("StyledText"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cp = jf.getContentPane(); JTextPane pane = new JTextPane(); SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setBold(set, true); // Set the attributes before adding text pane.setCharacterAttributes(set, true); pane.setText("java2s.com "); set = new SimpleAttributeSet(); StyleConstants.setItalic(set, true); StyleConstants.setForeground(set, Color.red); StyleConstants.setBackground(set, Color.blue); Document doc = pane.getStyledDocument(); doc.insertString(doc.getLength(), "Swing ", set); set = new SimpleAttributeSet(); StyleConstants.setFontSize(set, 24); doc.insertString(doc.getLength(), "Tutorial", set); JScrollPane scrollPane = new JScrollPane(pane); cp.add(scrollPane, BorderLayout.CENTER); jf.setSize(400, 300);/* w w w . j av a 2 s . com*/ jf.setVisible(true); }
From source file:AddingDocumentListenerJTextFieldSample.java
public static void main(String args[]) { final JFrame frame = new JFrame("Default Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField(); frame.add(textField, BorderLayout.NORTH); DocumentListener documentListener = new DocumentListener() { public void changedUpdate(DocumentEvent documentEvent) { printIt(documentEvent);/* ww w.jav a 2s . c o m*/ } public void insertUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void removeUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } private void printIt(DocumentEvent documentEvent) { DocumentEvent.EventType type = documentEvent.getType(); String typeString = null; if (type.equals(DocumentEvent.EventType.CHANGE)) { typeString = "Change"; } else if (type.equals(DocumentEvent.EventType.INSERT)) { typeString = "Insert"; } else if (type.equals(DocumentEvent.EventType.REMOVE)) { typeString = "Remove"; } System.out.print("Type : " + typeString); Document source = documentEvent.getDocument(); int length = source.getLength(); System.out.println("Length: " + length); } }; textField.getDocument().addDocumentListener(documentListener); frame.setSize(250, 150); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField nameTextField = new JTextField(); frame.add(nameTextField, BorderLayout.NORTH); frame.add(new JTextField(), BorderLayout.SOUTH); DocumentListener documentListener = new DocumentListener() { public void changedUpdate(DocumentEvent documentEvent) { printIt(documentEvent);/*from w ww. j a v a2 s .c o m*/ } public void insertUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void removeUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } private void printIt(DocumentEvent documentEvent) { DocumentEvent.EventType type = documentEvent.getType(); String typeString = null; if (type.equals(DocumentEvent.EventType.CHANGE)) { typeString = "Change"; } else if (type.equals(DocumentEvent.EventType.INSERT)) { typeString = "Insert"; } else if (type.equals(DocumentEvent.EventType.REMOVE)) { typeString = "Remove"; } System.out.print("Type : " + typeString + " / "); Document source = documentEvent.getDocument(); int length = source.getLength(); try { System.out.println("Contents: " + source.getText(0, length)); } catch (BadLocationException badLocationException) { System.out.println("Contents: Unknown"); } } }; nameTextField.getDocument().addDocumentListener(documentListener); frame.setSize(250, 100); frame.setVisible(true); }
From source file:Main.java
public static void highlight(JTextComponent textComp, String pattern) throws Exception { removeHighlights(textComp);/*from w w w. ja v a2s . com*/ Highlighter hilite = textComp.getHighlighter(); Document doc = textComp.getDocument(); String text = doc.getText(0, doc.getLength()); int pos = 0; while ((pos = text.indexOf(pattern, pos)) >= 0) { hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter); pos += pattern.length(); } }
From source file:Main.java
public static String getDocumentText(final Document document) { try {/*from w w w.jav a 2s .c o m*/ return document.getText(0, document.getLength()); } catch (BadLocationException e) { throw new RuntimeException(e); } }
From source file:Main.java
protected static int getLineOfOffset(int offset, Document doc) throws BadLocationException { if (offset < 0 || doc == null) { throw new BadLocationException("", -1); } else if (offset > doc.getLength()) { throw new BadLocationException("", doc.getLength() + 1); } else {//from w w w .ja v a 2s .co m Element map = doc.getDefaultRootElement(); return map.getElementIndex(offset); } }
From source file:com.mirth.connect.server.userutil.FileUtil.java
/** * Converts an RTF into plain text using the Swing RTFEditorKit. * /* w w w. j a v a 2 s .c o m*/ * @param message * The RTF message to convert. * @param replaceLinebreaksWith * If not null, any line breaks in the converted message will be replaced with this * string. * @return The converted plain text message. * @throws IOException * @throws BadLocationException */ public static String rtfToPlainText(String message, String replaceLinebreaksWith) throws IOException, BadLocationException { String convertedPlainText; // Reading the RTF content string Reader in = new StringReader(message); // creating a default blank styled document DefaultStyledDocument styledDoc = new DefaultStyledDocument(); // Creating a RTF Editor kit RTFEditorKit rtfKit = new RTFEditorKit(); // Populating the contents in the blank styled document rtfKit.read(in, styledDoc, 0); // Getting the root document Document doc = styledDoc.getDefaultRootElement().getDocument(); convertedPlainText = doc.getText(0, doc.getLength()); if (replaceLinebreaksWith != null) { convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith); } return convertedPlainText; }
From source file:TextUtilities.java
/** * Returns the offset of the bracket matching the one at the specified offset * of the document, or -1 if the bracket is unmatched (or if the character is * not a bracket).//from ww w. j ava 2s .com * * @param doc * The document * @param offset * The offset * @exception BadLocationException * If an out-of-bounds access was attempted on the document * text */ public static int findMatchingBracket(Document doc, int offset) throws BadLocationException { if (doc.getLength() == 0) return -1; char c = doc.getText(offset, 1).charAt(0); char cprime; // c` - corresponding character boolean direction; // true = back, false = forward switch (c) { case '(': cprime = ')'; direction = false; break; case ')': cprime = '('; direction = true; break; case '[': cprime = ']'; direction = false; break; case ']': cprime = '['; direction = true; break; case '{': cprime = '}'; direction = false; break; case '}': cprime = '{'; direction = true; break; default: return -1; } int count; // How to merge these two cases is left as an exercise // for the reader. // Go back or forward if (direction) { // Count is 1 initially because we have already // `found' one closing bracket count = 1; // Get text[0,offset-1]; String text = doc.getText(0, offset); // Scan backwards for (int i = offset - 1; i >= 0; i--) { // If text[i] == c, we have found another // closing bracket, therefore we will need // two opening brackets to complete the // match. char x = text.charAt(i); if (x == c) count++; // If text[i] == cprime, we have found a // opening bracket, so we return i if // --count == 0 else if (x == cprime) { if (--count == 0) return i; } } } else { // Count is 1 initially because we have already // `found' one opening bracket count = 1; // So we don't have to + 1 in every loop offset++; // Number of characters to check int len = doc.getLength() - offset; // Get text[offset+1,len]; String text = doc.getText(offset, len); // Scan forwards for (int i = 0; i < len; i++) { // If text[i] == c, we have found another // opening bracket, therefore we will need // two closing brackets to complete the // match. char x = text.charAt(i); if (x == c) count++; // If text[i] == cprime, we have found an // closing bracket, so we return i if // --count == 0 else if (x == cprime) { if (--count == 0) return i + offset; } } } // Nothing found return -1; }
From source file:com.mirth.connect.server.util.FileUtil.java
public static String rtfToPlainText(String message, String replaceLinebreaksWith) throws IOException, BadLocationException { String convertedPlainText;// ww w . j a v a2s. c o m // Reading the RTF content string Reader in = new StringReader(message); // creating a default blank styled document DefaultStyledDocument styledDoc = new DefaultStyledDocument(); // Creating a RTF Editor kit RTFEditorKit rtfKit = new RTFEditorKit(); // Populating the contents in the blank styled document rtfKit.read(in, styledDoc, 0); // Getting the root document Document doc = styledDoc.getDefaultRootElement().getDocument(); convertedPlainText = doc.getText(0, doc.getLength()); if (replaceLinebreaksWith != null) { convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith); } return convertedPlainText; }