List of usage examples for javax.swing JTextPane getStyledDocument
public StyledDocument getStyledDocument()
From source file:Main.java
public static void main(String[] args) { JTextPane textPane = new JTextPane(); textPane.setText("This is a test string"); StyleConstants.setBold(BOLD, true); StyleConstants.setItalic(ITALIC, true); int start = 5; int end = 10; textPane.getStyledDocument().setCharacterAttributes(start, end - start, BOLD, false); textPane.getStyledDocument().setCharacterAttributes(start, end - start, ITALIC, false); for (int i = start; i < end; i++) System.out.println(// w w w. j ava 2s. c om textPane.getStyledDocument().getCharacterElement(i).getAttributes().containsAttributes(BOLD)); // all now print true JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(textPane)); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); JTextPane textPane = new JTextPane(); JButton button = new JButton("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.setLayout(new BorderLayout()); panel.setPreferredSize(new Dimension(200, 200)); panel.add(textPane, BorderLayout.CENTER); panel.add(button, BorderLayout.SOUTH); textPane.addStyle("myStyle", null); button.addActionListener(e -> {/*from w ww. j a v a 2 s.c om*/ StyledDocument doc = textPane.getStyledDocument(); int start = textPane.getSelectionStart(); int end = textPane.getSelectionEnd(); if (start == end) { return; } if (start > end) { int life = start; start = end; end = life; } Style style = textPane.getStyle("myStyle"); if (StyleConstants.isBold(style)) { StyleConstants.setBold(style, false); } else { StyleConstants.setBold(style, true); } doc.setCharacterAttributes(start, end - start, style, false); }); frame.add(panel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }
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);/* www . j av a 2 s. com*/ jf.setVisible(true); }
From source file:Main.java
public static void main(String args[]) throws BadLocationException { JTextPane textPane1 = new JTextPane(); MutableAttributeSet black = new SimpleAttributeSet(); MutableAttributeSet red = new SimpleAttributeSet(); StyleConstants.setForeground(black, Color.black); StyleConstants.setForeground(red, Color.red); textPane1.setEditorKit(new StyledEditorKit()); doc1 = textPane1.getDocument();/* w w w . ja va 2s . c o m*/ append1("This is a Test!\n"); attribute = red; append1("Hello world! Hello Stackoverflow\n"); attribute = black; append1("the text is black again\n"); StyledDocument styledDocument = textPane1.getStyledDocument(); Element element; JTextPane textPane2 = new JTextPane(); textPane2.setEditorKit(new StyledEditorKit()); doc2 = textPane2.getDocument(); for (int i = 0; i < styledDocument.getLength(); i++) { element = styledDocument.getCharacterElement(i); AttributeSet attributeNew = element.getAttributes(); System.out.println(i); append2(styledDocument.getText(i, 1), attributeNew); } JFrame frame1 = new JFrame(); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setSize(400, 300); frame1.getContentPane().add(new JScrollPane(textPane1), BorderLayout.CENTER); frame1.setVisible(true); JFrame frame2 = new JFrame(); frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame2.setSize(400, 300); frame2.setLocation(300, 0); frame2.getContentPane().add(new JScrollPane(textPane2), BorderLayout.CENTER); frame2.setVisible(true); }
From source file:DocumentModel.java
public static void main(String[] args) { final StyledDocument doc; final JTextPane textpane; JFrame f = new JFrame(); f.setTitle("Document Model"); JToolBar toolbar = new JToolBar(); JButton boldb = new JButton("bold"); JButton italb = new JButton("italic"); JButton strib = new JButton("strike"); JButton undeb = new JButton("underline"); toolbar.add(boldb);//w ww. ja va 2 s .c o m toolbar.add(italb); toolbar.add(strib); toolbar.add(undeb); f.add(toolbar, BorderLayout.NORTH); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); JScrollPane pane = new JScrollPane(); textpane = new JTextPane(); textpane.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); doc = textpane.getStyledDocument(); Style style = textpane.addStyle("Bold", null); StyleConstants.setBold(style, true); style = textpane.addStyle("Italic", null); StyleConstants.setItalic(style, true); style = textpane.addStyle("Underline", null); StyleConstants.setUnderline(style, true); style = textpane.addStyle("Strike", null); StyleConstants.setStrikeThrough(style, true); boldb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doc.setCharacterAttributes(textpane.getSelectionStart(), textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Bold"), false); } }); italb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doc.setCharacterAttributes(textpane.getSelectionStart(), textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Italic"), false); } }); strib.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doc.setCharacterAttributes(textpane.getSelectionStart(), textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Strike"), false); } }); undeb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doc.setCharacterAttributes(textpane.getSelectionStart(), textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Underline"), false); } }); pane.getViewport().add(textpane); panel.add(pane); f.add(panel); f.setSize(new Dimension(380, 320)); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.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.ja va 2 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:Main.java
private static AttributeSet getAttributes(MouseEvent e) { JTextPane text = (JTextPane) e.getSource(); Point mouseLocation = new Point(e.getX(), e.getY()); int pos = text.viewToModel(mouseLocation); if (pos >= 0) { try {//from ww w . j av a 2 s .c o m Rectangle rect = text.modelToView(pos); int lowerCorner = rect.y + rect.height; if (e.getX() < rect.x && e.getY() < lowerCorner && pos > 0) { pos--; } } catch (BadLocationException ex) { ex.printStackTrace(); } StyledDocument doc = text.getStyledDocument(); Element element = doc.getCharacterElement(pos); return element.getAttributes(); } return null; }
From source file:Main.java
public static void setTabs(JTextPane textPane, int charactersPerTab) { FontMetrics fm = textPane.getFontMetrics(textPane.getFont()); int charWidth = fm.charWidth('w'); int tabWidth = charWidth * charactersPerTab; TabStop[] tabs = new TabStop[5]; for (int i = 0; i < tabs.length; i++) { int tab = i + 1; tabs[i] = new TabStop(tab * tabWidth); }/*www . j a va 2s.c o m*/ TabSet tabSet = new TabSet(tabs); SimpleAttributeSet attributes = new SimpleAttributeSet(); StyleConstants.setTabSet(attributes, tabSet); int length = textPane.getDocument().getLength(); textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false); }
From source file:Main.java
public TextPaneAttributes() { JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); MutableAttributeSet standard = new SimpleAttributeSet(); StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, 0, standard, true); MutableAttributeSet keyWord = new SimpleAttributeSet(); StyleConstants.setForeground(keyWord, Color.red); StyleConstants.setItalic(keyWord, true); textPane.setText("this is a test. \nthis is a four."); doc.setCharacterAttributes(0, 3, keyWord, false); doc.setCharacterAttributes(19, 4, keyWord, false); try {/* www. j a v a 2s . c om*/ doc.insertString(0, "Start of text\n", null); doc.insertString(doc.getLength(), "End of text\n", keyWord); } catch (Exception e) { } MutableAttributeSet selWord = new SimpleAttributeSet(); StyleConstants.setForeground(selWord, Color.RED); StyleConstants.setItalic(selWord, true); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setPreferredSize(new Dimension(200, 200)); add(scrollPane); JButton toggleButton = new JButton("Find 'four'"); toggleButton.addActionListener(e -> { int index = textPane.getText().indexOf("four"); StyledDocument doc1 = textPane.getStyledDocument(); doc1.setCharacterAttributes(index, 4, selWord, false); }); add(toggleButton, BorderLayout.SOUTH); }
From source file:edu.scripps.fl.pubchem.xmltool.gui.GUIComponent.java
public JTextPane createJTextPane() { String newline = "\n"; String[] description = { "PubChem XML Tool (Version: " + version + ")" + newline, "By: S. Canny (scanny@scripps.edu) and M. Southern (southern@scripps.edu)" + newline, "" + newline, "PubChem XML Tool main functions:" + newline, "1. Create a PubChem XML that can include Assay, Result TIDs, Xrefs, Panel, and Categorized Comments." + newline,/*from w ww .j a v a 2 s . c o m*/ "2. Extract Assay, Result TID, Xref, Panel, and Categorized Comment information from a PubChem XML." + newline, "3. Create a report from an Excel workbook or PubChem XMLs." + newline, "" + newline, "Other features:" + newline, "1. Automatically adds reference section to description of PubChem XML or a report if placeholder is used." + newline, "2. Checks proteins, genes, omims, and taxonomies for connections when creating PubChem XML or a report." + newline, "3. Can retrieve on-hold and newly deposited assays from deposition system to extract or create report." + newline, "" + newline, "\t\t\t(c) 2010, The Scripps Research Institute- Florida" }; String[] styles = { "bold", "regular", "regular", "regular", "regular", "regular", "regular", "regular", "regular", "regular", "regular", "regular", "regular", "regular", "right" }; JTextPane jtp = new JTextPane(); StyledDocument doc = jtp.getStyledDocument(); addStylesToDocument(doc); try { for (int ii = 0; ii < description.length; ii++) doc.insertString(doc.getLength(), description[ii], doc.getStyle(styles[ii])); } catch (BadLocationException ble) { log.error(ble.getMessage(), ble); } jtp.setOpaque(false); jtp.setEditable(false); jtp.setPreferredSize(new Dimension(640, 230)); return jtp; }