Example usage for javax.swing.text StyleContext getDefaultStyleContext

List of usage examples for javax.swing.text StyleContext getDefaultStyleContext

Introduction

In this page you can find the example usage for javax.swing.text StyleContext getDefaultStyleContext.

Prototype

public static final StyleContext getDefaultStyleContext() 

Source Link

Document

Returns default AttributeContext shared by all documents that don't bother to define/supply their own context.

Usage

From source file:Main.java

public static void main(String... args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.RED);
    aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");

    JTextPane tPane1 = new JTextPane();
    tPane1.setCharacterAttributes(aset, false);

    f.add(tPane1, BorderLayout.CENTER);

    f.pack();/*from  www.  ja  v a2s  . c o m*/
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextPane pane = new JTextPane();
    TabStop[] tabs = new TabStop[1];
    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);/*ww  w  .  j a va2  s  .c  o m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextPane pane = new JTextPane();
    TabStop[] tabs = new TabStop[2];
    tabs[0] = new TabStop(60, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
    tabs[1] = new TabStop(100, TabStop.ALIGN_LEFT, 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\tValue\n" + "\t200.002\t200.002\t200.002\t200.002\n");

    JFrame d = new JFrame();
    d.setContentPane(new JScrollPane(pane));
    d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    d.setSize(360, 120);//w w w . j a  va 2  s. c om
    d.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int SIZE = 14;
    String FONT = "Dialog";

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    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 = "this is bold test";
    String plainText = "this is plain.";

    doc.insertString(doc.getLength(), boldText, boldStyle);
    doc.insertString(doc.getLength(), plainText, defaultStyle);

    JPanel panel = new JPanel();
    panel.add(tp);//  w  ww  . ja v a2 s  . com

    JComboBox<String> zoomCombo = new JComboBox<String>(
            new String[] { "0.75", "1.00", "1.50", "1.75", "2.00" });
    zoomCombo.addActionListener(e -> {
        String s = (String) zoomCombo.getSelectedItem();
        double scale = new Double(s).doubleValue();
        int size = (int) (SIZE * scale);
        tp.setFont(new Font(FONT, Font.PLAIN, size));
    });
    zoomCombo.setSelectedItem("1.00");
    JPanel optionsPanel = new JPanel();
    optionsPanel.add(zoomCombo);
    panel.setBackground(Color.WHITE);
    frame.add(panel, BorderLayout.CENTER);
    frame.add(optionsPanel, BorderLayout.NORTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:TabExample.java

public static void main(String[] args) {

    JTextPane pane = new JTextPane();
    TabStop[] tabs = new TabStop[4];
    tabs[0] = new TabStop(60, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
    tabs[1] = new TabStop(100, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
    tabs[2] = new TabStop(200, TabStop.ALIGN_CENTER, TabStop.LEAD_NONE);
    tabs[3] = new TabStop(300, TabStop.ALIGN_DECIMAL, 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);//  w w  w  . j  ava 2s  . c om
    frame.setVisible(true);
}

From source file:Main.java

private static void appendToPane(JTextPane tp, String msg, Color f, Color b) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, f);
    aset = sc.addAttribute(aset, StyleConstants.Background, b);

    aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
    aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

    int len = tp.getDocument().getLength();
    tp.setCaretPosition(len);//from www . ja v a2 s  . c o m
    tp.setCharacterAttributes(aset, false);
    tp.replaceSelection(msg);
}

From source file:Main.java

public void append(Color c, String s) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

    int len = getDocument().getLength();
    setCaretPosition(len);/* w  w  w.j  a va  2  s . c om*/
    setCharacterAttributes(aset, false);
    replaceSelection(s);
}

From source file:ParenMatcher.java

public ParenMatcher() {
    // create an array of AttributeSets from the array of Colors
    StyleContext sc = StyleContext.getDefaultStyleContext();
    badAttrSet = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, badColor);
    matchAttrSet = new AttributeSet[matchColor.length];
    for (int j = 0; j < matchColor.length; j += 1)
        matchAttrSet[j] = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, matchColor[j]);
}

From source file:Main.java

private void appendToPane(JTextPane tp, String msg, Color c) {
    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);

    int len = tp.getDocument().getLength();
    tp.setCaretPosition(len);/*from   w  ww.  j a  v a  2 s .  c o m*/
    tp.setCharacterAttributes(aset, false);
    tp.replaceSelection(msg);
}

From source file:edu.scripps.fl.pubchem.xmltool.gui.GUIComponent.java

protected void addStylesToDocument(StyledDocument doc) {
    //Initialize some styles.
    Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

    Style regular = doc.addStyle("regular", def);
    StyleConstants.setFontFamily(def, "SansSerif");

    Style s = doc.addStyle("italic", regular);
    StyleConstants.setItalic(s, true);

    s = doc.addStyle("bold", regular);
    StyleConstants.setBold(s, true);

    s = doc.addStyle("right", regular);
    StyleConstants.setAlignment(s, StyleConstants.ALIGN_RIGHT);
}