setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); tPane = new JTextPane(); tPane.setBorder(eb); topPanel.add(tPane); appendToPane(tPane, "flow", Color.ORANGE); getContentPane().add(topPanel); pack(); setVisible(true); StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c); aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console"); aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED); tp.setCharacterAttributes(aset, false); tp.replaceSelection(msg);
protected void addStylesToDocument(JTextPane outputArea) { StyledDocument doc = outputArea.getStyledDocument(); Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style regular = doc.addStyle("regular", def); StyleConstants.setFontFamily(def, "Monospaced"); promptStyle = doc.addStyle("prompt", regular); StyleConstants.setForeground(promptStyle, Color.BLUE); commandStyle = doc.addStyle("command", regular); StyleConstants.setForeground(commandStyle, Color.MAGENTA); outputStyle = doc.addStyle("output", regular); StyleConstants.setBold(outputStyle, true); }
private void init() { StyleContext styleContext = StyleContext.getDefaultStyleContext(); Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE); Style comment = styleContext.addStyle(COMMENT, defaultStyle); StyleConstants.setForeground(comment, COMMENT_COLOR); StyleConstants.setItalic(comment, true); Style quotes = styleContext.addStyle(QUOTES, defaultStyle); StyleConstants.setForeground(quotes, Color.MAGENTA.darker().darker()); Style charQuotes = styleContext.addStyle(SINGLE_QUOTES, defaultStyle); StyleConstants.setForeground(charQuotes, Color.GREEN.darker().darker()); Style slashyQuotes = styleContext.addStyle(SLASHY_QUOTES, defaultStyle); StyleConstants.setForeground(slashyQuotes, Color.ORANGE.darker()); Style digit = styleContext.addStyle(DIGIT, defaultStyle); StyleConstants.setForeground(digit, Color.RED.darker()); Style operation = styleContext.addStyle(OPERATION, defaultStyle); StyleConstants.setBold(operation, true); Style ident = styleContext.addStyle(IDENT, defaultStyle); Style reservedWords = styleContext.addStyle(RESERVED_WORD, defaultStyle); StyleConstants.setBold(reservedWords, true); StyleConstants.setForeground(reservedWords, Color.BLUE.darker().darker()); Style leftParens = styleContext.addStyle(IDENT, defaultStyle);
LexerNode(boolean isParent) { StyleContext sc = StyleContext.getDefaultStyleContext(); defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE); }
JScrollPane scrollPane = new JScrollPane(textArea); scrollPane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JTextArea"), scrollPane.getBorder())); centerPanel.add(myTextBox.getMainComponent()); centerPanel.add(scrollPane); bottomPanel.add(new JButton(new AbstractAction("Submit Text") { TextBoxTest textBoxTester = new TextBoxTest(); + "This is your message!!\n\n Message \n\n Message \n\n" + "This is your message!!\n\n Message \n\n Message \n\n"; private StyledDocument doc = textPane.getStyledDocument(); scrollPane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JTextPane"), scrollPane.getBorder())); Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE); StyleConstants.setFontFamily(def, "SansSerif"); StyleConstants.setFontSize(regular, 16); StyleConstants.setForeground(s, Color.red);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tPane = new JTextPane(); tPane.setBorder(eb); tPane.setMargin(new Insets(5, 5, 5, 5)); JScrollPane scroller = new JScrollPane(); scroller.setViewportView(tPane); getContentPane().add(scroller, BorderLayout.CENTER); getContentPane().add(tfield, BorderLayout.PAGE_END); setSize(200, 100); setVisible(true); tfield.requestFocusInWindow(); StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c); aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console"); aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED); tp.setCharacterAttributes(aset, false); tp.replaceSelection(msg);
+ "<span id='xx' style='font-family:consolas'>Hola! consolas</span><br/>" + "<span id='xk' style='font-family:tahoma'>Hola! tahoma</span>"; private final JTextPane textPane1 = new JTextPane(); private final JTextPane textPane2 = new JTextPane(); private JComponent makeUI() { textPane1.setContentType("text/html"); textPane2.setFont(new Font("courier new", Font.PLAIN, 32)); textPane2.setDocument(new HTMLDocument() { @Override public Font getFont(AttributeSet attr) { StyleContext styles = (StyleContext) getAttributeContext(); Font f = styles.getFont(attr); String ff = f.getFamily(); System.out.println(ff); p.add(new JScrollPane(textPane1)); p.add(new JScrollPane(textPane2)); return p; JFrame f = new JFrame(); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.getContentPane().add(new IgnoreStyleTest().makeUI()); f.setSize(320, 240); f.setLocationRelativeTo(null); f.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 400); setLocationRelativeTo(null); final StyleContext cont = StyleContext.getDefaultStyleContext(); final AttributeSet attr = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED); final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK); DefaultStyledDocument doc = new DefaultStyledDocument() { public void insertString (int offset, String str, AttributeSet a) throws BadLocationException { JTextPane txt = new JTextPane(doc); txt.setText("public class Hi {}"); add(new JScrollPane(txt)); setVisible(true);
} catch (Exception evt) {} JFrame f = new JFrame("Styles Example 6"); StyleContext sc = new StyleContext(); final DefaultStyledDocument doc = new DefaultStyledDocument(sc); final JTextPane pane = new JTextPane(doc); Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE); final Style mainStyle = sc.addStyle("MainStyle", defaultStyle); StyleConstants.setLeftIndent(mainStyle, 16); StyleConstants.setRightIndent(mainStyle, 16); StyleConstants.setFirstLineIndent(mainStyle, 16); StyleConstants.setFontFamily(mainStyle, "serif"); StyleConstants.setFontSize(mainStyle, 12); final Style cwStyle = sc.addStyle("ConstantWidth", null); StyleConstants.setFontFamily(cwStyle, "monospaced"); StyleConstants.setForeground(cwStyle, Color.green); final Style heading2Style = sc.addStyle("Heading2", null); StyleConstants.setForeground(heading2Style, Color.red); StyleConstants.setFontSize(heading2Style, 16); pane.setForeground(Color.pink); pane.setFont(new Font("Monospaced", Font.ITALIC, 24)); } catch (BadLocationException e) {
JFrame frame = new JFrame("JTextPane Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); textPane = new JTextPane(styledDocument); textPane.addCaretListener(new SelectedText()); textPane.setPreferredSize(new Dimension(250, 125)); JScrollPane scrollPane = new JScrollPane(textPane); frame.add(scrollPane); frame.pack(); frame.setVisible(true); Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE); Style s = styledDocument.addStyle("bold", def); StyleConstants.setBold(s, true);
JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(600,200)); frame.getContentPane().setBackground(Color.BLACK); panel.setLayout(new GridLayout(1, 2)); panel.setBackground(Color.BLACK); frame.add(panel); StyleContext context = new StyleContext(); StyledDocument document = new DefaultStyledDocument(context); Style style = context.getStyle(StyleContext.DEFAULT_STYLE); StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT); JTextPane area = new JTextPane(document); panel.add(area); frame.setVisible(true);
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; public class SQLEditor extends JFrame { private static final long serialVersionUID = 1L; public SQLEditor() { StyleContext styleContext = new StyleContext(); Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE); Style cwStyle = styleContext.addStyle("ConstantWidth", null); StyleConstants.setForeground(cwStyle, Color.BLUE); StyleConstants.setBold(cwStyle, true); final JTextPane pane = new JTextPane(new KeywordStyledDocument(defaultStyle, cwStyle)); pane.setFont(new Font("Courier New", Font.PLAIN, 12)); JScrollPane scrollPane = new JScrollPane(pane); getContentPane().add(scrollPane, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(375, 400); } public static void main(String[] args) throws BadLocationException { SQLEditor app = new SQLEditor(); app.setVisible(true); } }
JFrame f = new JFrame("Styles Example 1"); StyleContext sc = new StyleContext(); final DefaultStyledDocument doc = new DefaultStyledDocument(sc); JTextPane pane = new JTextPane(doc); final Style heading2Style = sc.addStyle("Heading2", null); heading2Style.addAttribute(StyleConstants.Foreground, Color.red); heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16)); f.getContentPane().add(new JScrollPane(pane)); f.setSize(400, 300); f.setVisible(true);
public static JScrollPane createJTextPane(){ screen = new JTextPane(); screen.setFocusable(false); screen.setEditable(false); screen.setAutoscrolls(true); JScrollPane editorScrollPane = new JScrollPane(screen); editorScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); editorScrollPane.setPreferredSize(new Dimension(480, 240)); editorScrollPane.setMinimumSize(new Dimension(320, 160)); StyledDocument doc = screen.getStyledDocument(); Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Random rnd = new Random(); Style receivedigitalReadResultStyle = doc.addStyle("receivedigitalReadResultStyle", def); StyleConstants.setForeground(receivedigitalReadResultStyle, new Color(rnd.nextInt(176), rnd.nextInt(176), rnd.nextInt(176))); Style receiveanalogReadResultStyle = doc.addStyle("receiveanalogReadResultStyle", def); StyleConstants.setForeground(receiveanalogReadResultStyle, new Color(rnd.nextInt(176), rnd.nextInt(176), rnd.nextInt(176))); Style receivepongStyle = doc.addStyle("receivepongStyle", def); StyleConstants.setForeground(receivepongStyle, new Color(rnd.nextInt(176), rnd.nextInt(176), rnd.nextInt(176))); Style receiveinterruptNotificationStyle = doc.addStyle("receiveinterruptNotificationStyle", def); StyleConstants.setForeground(receiveinterruptNotificationStyle, new Color(rnd.nextInt(176), rnd.nextInt(176), rnd.nextInt(176))); Style receiveeeprom_valueStyle = doc.addStyle("receiveeeprom_valueStyle", def); StyleConstants.setForeground(receiveeeprom_valueStyle, new Color(rnd.nextInt(176), rnd.nextInt(176), rnd.nextInt(176))); Style receiveeeprom_write_ackStyle = doc.addStyle("receiveeeprom_write_ackStyle", def); StyleConstants.setForeground(receiveeeprom_write_ackStyle, new Color(rnd.nextInt(176), rnd.nextInt(176), rnd.nextInt(176))); return editorScrollPane; }
JFrame frame = new JFrame(); DefaultStyledDocument document = new DefaultStyledDocument(); JTextPane pane = new JTextPane(document); JPanel mainPanel = new JPanel(); JButton button = new JButton("Button"); button.setPreferredSize(new Dimension(100, 40)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane.setPreferredSize(new Dimension(200, 200)); mainPanel.add(button); frame.getContentPane().add(pane, BorderLayout.CENTER); frame.getContentPane().add(mainPanel, BorderLayout.WEST); StyleContext context = new StyleContext(); Style style = context.addStyle("test", null); StyleConstants.setForeground(style, Color.BLACK); document.insertString(0, "Four: success \n", style); StyleConstants.setForeground(style, Color.RED); document.insertString(0, "Three: error \n", style); document.insertString(0, "Two: error \n", style); StyleConstants.setForeground(style, Color.BLACK); frame.pack(); frame.setVisible(true);
public ChangeFontColor() { super("Change Font Color"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(1100, 700); setLocationRelativeTo(null); final StyleContext cont = StyleContext.getDefaultStyleContext(); final AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED); final AttributeSet attrYel = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.YELLOW); final AttributeSet attrBlu = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE); final AttributeSet attrWhite = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.WHITE); JTextPane txt = new JTextPane(doc); txt.setBackground(Color.DARK_GRAY); txt.setForeground(Color.WHITE); txt.setCaretColor(Color.WHITE); txt.setFont(new Font("Serif", Font.PLAIN, 18)); add(new JScrollPane(txt)); setVisible(true);
JFrame frame = new JFrame("ScaledJTextPane using BufferedImage"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextPane tp = new JTextPane(); tp.setFont(new Font(FONT, Font.PLAIN, SIZE)); tp.setPreferredSize(new Dimension(400, 300)); StyledDocument doc = tp.getStyledDocument(); Style defaultStyle = StyleContext.getDefaultStyleContext() .getStyle(StyleContext.DEFAULT_STYLE); Style boldStyle = doc.addStyle("bold", defaultStyle); StyleConstants.setBold(boldStyle, true); String boldText = "Four score and seven years ago "; String plainText = "our fathers brought forth on this continent, " panel.add(tp); double scale = new Double(s).doubleValue() / 100; int size = (int) (SIZE * scale); tp.setFont(new Font(FONT, Font.PLAIN, size)); optionsPanel.add(zoomCombo); panel.setBackground(Color.WHITE); frame.add(panel, BorderLayout.CENTER); frame.add(optionsPanel, BorderLayout.NORTH); frame.pack(); frame.setVisible(true);
JFrame frame = new JFrame(); textPane.getColoredWordsMap().put("WHERE", Color.GREEN); frame.getContentPane().add(textPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); setBackground(Color.GRAY); getDocument().addDocumentListener(new DocumentListener() { @Override StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color); int length = textPane.getDocument().getLength(); textPane.setCaretPosition(length); textPane.setCharacterAttributes(aset, false); textPane.replaceSelection(text);
topPanel.setLayout(new GridLayout(1, 2)); bottomPanel.setBackground(Color.BLACK); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLUE); aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console"); tPane1 = new JTextPane(); tPane1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY)); tPane1.setCharacterAttributes(aset, false); // Add these settings to the other JTextPane also tPane2 = new JTextPane(); tPane2.setCharacterAttributes(aset, false); // Mimic what the other JTextPane has button.setBackground(Color.DARK_GRAY); button.setForeground(Color.WHITE); button.addActionListener(new ActionListener() add(topPanel, BorderLayout.CENTER); add(bottomPanel, BorderLayout.PAGE_END); topPanel.add(tPane1); bottomPanel.add(button);
JTextPane pane = new JTextPane(); TabStop[] tabs = new TabStop[4]; tabs[0] = new TabStop(60, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE); TabSet tabset = new TabSet(tabs); StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset); pane.setParagraphAttributes(aset, false); pane.setText("\tright\tleft\tcenter\tdecimal\n" + "\t1\t1\t1\t1.0\n" + "\t200.002\t200.002\t200.002\t200.002\n" + "\t.33\t.33\t.33\t.33\n"); JFrame frame = new JFrame("TabExample"); frame.setContentPane(new JScrollPane(pane)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(360, 120); frame.setVisible(true);