List of usage examples for javax.swing.text StyledDocument getLength
public int getLength();
From source file:TextTabSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Tab Attributes"); Container content = frame.getContentPane(); StyledDocument document = new DefaultStyledDocument(); int positions[] = { TabStop.ALIGN_BAR, TabStop.ALIGN_CENTER, TabStop.ALIGN_DECIMAL, TabStop.ALIGN_LEFT, TabStop.ALIGN_RIGHT }; String strings[] = { "\tBAR\n", "\tCENTER\n", "\t3.14159265\n", "\tLEFT\n", "\tRIGHT\n" }; SimpleAttributeSet attributes = new SimpleAttributeSet(); for (int i = 0, n = positions.length; i < n; i++) { TabStop tabstop = new TabStop(150, positions[i], TabStop.LEAD_DOTS); try {//w w w .ja v a 2s .c o m int position = document.getLength(); document.insertString(position, strings[i], null); TabSet tabset = new TabSet(new TabStop[] { tabstop }); StyleConstants.setTabSet(attributes, tabset); document.setParagraphAttributes(position, 1, attributes, false); } catch (BadLocationException badLocationException) { System.err.println("Oops"); } } JTextPane textPane = new JTextPane(document); textPane.setEditable(false); JScrollPane scrollPane = new JScrollPane(textPane); content.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Tab Attributes"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); StyledDocument document = new DefaultStyledDocument(); int positions[] = { TabStop.ALIGN_BAR, TabStop.ALIGN_CENTER, TabStop.ALIGN_DECIMAL, TabStop.ALIGN_LEFT, TabStop.ALIGN_RIGHT }; String strings[] = { "\tTabStop.ALIGN_BAR\n", "\tTabStop.ALIGN_CENTER\n", "\tTabStop.ALIGN_DECIMAL\n", "\tTabStop.ALIGN_LEFT\n", "\tTabStop.ALIGN_RIGHT\n" }; SimpleAttributeSet attributes = new SimpleAttributeSet(); for (int i = 0, n = positions.length; i < n; i++) { TabStop tabstop = new TabStop(150, positions[i], TabStop.LEAD_DOTS); try {/* www . j av a2 s.c om*/ int position = document.getLength(); document.insertString(position, strings[i], null); TabSet tabset = new TabSet(new TabStop[] { tabstop }); StyleConstants.setTabSet(attributes, tabset); document.setParagraphAttributes(position, 1, attributes, false); } catch (BadLocationException badLocationException) { System.err.println("Bad Location"); } } JTextPane textPane = new JTextPane(document); textPane.setEditable(false); JScrollPane scrollPane = new JScrollPane(textPane); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) throws Exception { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String s = new Date().toString(); JTextPane jtp = new JTextPane(); StyledDocument doc = (StyledDocument) jtp.getDocument(); SimpleAttributeSet normal = new SimpleAttributeSet(); StyleConstants.setFontFamily(normal, "SansSerif"); StyleConstants.setFontSize(normal, 16); SimpleAttributeSet boldBlue = new SimpleAttributeSet(normal); StyleConstants.setBold(boldBlue, true); StyleConstants.setForeground(boldBlue, Color.blue); SimpleAttributeSet highAlert = new SimpleAttributeSet(boldBlue); StyleConstants.setFontSize(highAlert, 18); StyleConstants.setItalic(highAlert, true); StyleConstants.setForeground(highAlert, Color.red); doc.insertString(doc.getLength(), s + "\n", normal); doc.insertString(doc.getLength(), s + "\n", boldBlue); doc.insertString(doc.getLength(), s + "\n", highAlert); f.add(jtp);//from w ww. j a va 2 s . c om f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); }
From source file:StyledSample.java
public static void main(String args[]) { String BOLD_ITALIC = "BoldItalic"; String GRAY_PLAIN = "Gray"; JFrame frame = new JFrame("Simple Attributes"); Container content = frame.getContentPane(); StyledDocument document = new DefaultStyledDocument(); Style style = (Style) document.getStyle(StyleContext.DEFAULT_STYLE); StyleConstants.setBold(style, true); StyleConstants.setItalic(style, true); document.addStyle(BOLD_ITALIC, null); // style = document.getStyle(StyleContext.DEFAULT_STYLE); // StyleConstants.setBold(style, false); // StyleConstants.setItalic(style, false); // StyleConstants.setForeground(style, Color.lightGray); // document.addStyle(GRAY_PLAIN, null); style = document.getStyle(BOLD_ITALIC); // Insert content try {/* ww w . j a v a 2s . com*/ document.insertString(document.getLength(), "Hello Java\n", style); } catch (BadLocationException badLocationException) { System.err.println("Oops"); } style = document.getStyle(GRAY_PLAIN); // Insert content try { document.insertString(document.getLength(), " - Good-bye Visual Basic\n", style); } catch (BadLocationException badLocationException) { System.err.println("Oops"); } JTextPane textPane = new JTextPane(document); textPane.setEditable(false); JScrollPane scrollPane = new JScrollPane(textPane); content.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { JFrame frame = new JFrame("TextPane Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); StyleContext context = new StyleContext(); StyledDocument document = new DefaultStyledDocument(context); Style style = context.getStyle(StyleContext.DEFAULT_STYLE); StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT); StyleConstants.setFontSize(style, 14); StyleConstants.setSpaceAbove(style, 4); StyleConstants.setSpaceBelow(style, 4); SimpleAttributeSet attributes = new SimpleAttributeSet(); StyleConstants.setBold(attributes, true); StyleConstants.setItalic(attributes, true); // Third style for icon/component Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE); Icon icon = new ImageIcon("Computer.gif"); JLabel label = new JLabel(icon); StyleConstants.setComponent(labelStyle, label); try {/*from w w w .j a v a 2 s. com*/ document.insertString(document.getLength(), "Hello www.java2s.com", attributes); document.insertString(document.getLength(), "Ignored", labelStyle); } catch (BadLocationException badLocationException) { System.err.println("Oops"); } JTextPane textPane = new JTextPane(document); textPane.setEditable(false); JScrollPane scrollPane = new JScrollPane(textPane); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
/** * Affect a stylised text into an instance of JTextPane. * @param textPane Instance of JTextPane to affect. * @param textArray Array of strings./*from w w w .j a va2 s . c om*/ * @param styleArray Array of styles. Must match the textArray. */ public static void setText(JTextPane textPane, String[] textArray, String[] styleArray) { StyledDocument doc = textPane.getStyledDocument(); try { doc.remove(0, doc.getLength()); // Erase all the previous text. for (int i = 0; i < textArray.length; i++) { int offset = doc.getLength(); javax.swing.text.Style style = textPane.getStyle(styleArray[i]); doc.insertString(offset, textArray[i], style); doc.setParagraphAttributes(offset, textArray[i].length(), style, true); } textPane.setCaretPosition(0); } catch (BadLocationException ignore) { ignore.printStackTrace(); } }
From source file:com.keevosh.springframework.boot.netbeans.SpringBootConfigurationCompletionProvider.java
static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException { Element lineElement = doc.getParagraphElement(offset); int start = lineElement.getStartOffset(); while (start + 1 < lineElement.getEndOffset()) { try {/* ww w .j ava2s .com*/ if (doc.getText(start, 1).charAt(0) != ' ') { break; } } catch (BadLocationException ex) { throw (BadLocationException) new BadLocationException( "calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(), start).initCause(ex); } start++; } return start; }
From source file:Main.java
public Main() { setDefaultCloseOperation(DISPOSE_ON_CLOSE); setSize(400, 300);//w w w. jav a 2 s.c o m Exception ex = new Exception(); AttributeSet attr = null; StyledDocument doc = text.getStyledDocument(); for (StackTraceElement trace : ex.getStackTrace()) { try { doc.insertString(doc.getLength(), trace.toString() + '\n', attr); } catch (BadLocationException ex1) { ex1.printStackTrace(); } } getContentPane().add(new JScrollPane(text)); }
From source file:ParenMatcher.java
public void run() { StyledDocument doc = getStyledDocument(); String text = ""; int len = doc.getLength(); try {/*from ww w .j av a2s.co m*/ text = doc.getText(0, len); } catch (BadLocationException ble) { } java.util.Stack stack = new java.util.Stack(); for (int j = 0; j < text.length(); j += 1) { char ch = text.charAt(j); if (ch == '(' || ch == '[' || ch == '{') { int depth = stack.size(); stack.push("" + ch + j); // push a String containg the char and // the offset AttributeSet aset = matchAttrSet[depth % matchAttrSet.length]; doc.setCharacterAttributes(j, 1, aset, false); } if (ch == ')' || ch == ']' || ch == '}') { String peek = stack.empty() ? "." : (String) stack.peek(); if (matches(peek.charAt(0), ch)) { // does it match? stack.pop(); int depth = stack.size(); AttributeSet aset = matchAttrSet[depth % matchAttrSet.length]; doc.setCharacterAttributes(j, 1, aset, false); } else { // mismatch doc.setCharacterAttributes(j, 1, badAttrSet, false); } } } while (!stack.empty()) { // anything left in the stack is a mismatch String pop = (String) stack.pop(); int offset = Integer.parseInt(pop.substring(1)); doc.setCharacterAttributes(offset, 1, badAttrSet, false); } }
From source file:Main.java
public Main() throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(edit);//from w w w .j a va 2 s . c o m edit.setEditorKit(new MyEditorKit()); SimpleAttributeSet attrs = new SimpleAttributeSet(); StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_CENTER); StyledDocument doc = (StyledDocument) edit.getDocument(); doc.insertString(0, "111\n2222222\n3333", attrs); doc.setParagraphAttributes(0, doc.getLength() - 1, attrs, false); frame.setSize(300, 300); frame.setVisible(true); }