Example usage for javax.swing.text SimpleAttributeSet SimpleAttributeSet

List of usage examples for javax.swing.text SimpleAttributeSet SimpleAttributeSet

Introduction

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

Prototype

public SimpleAttributeSet() 

Source Link

Document

Creates a new attribute set.

Usage

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 www  .  j  a  v  a  2s .c  o m
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:SimpleAttributeBoldItalic.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Simple Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);

    try {/*w w  w.  j a va 2 s . c  o m*/
        document.insertString(document.getLength(), "Bold, Italic", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    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[]) {
    JFrame frame = new JFrame("Simple Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.LIGHT_GRAY);

    try {//from w w w.  java2 s . c  om
        document.insertString(document.getLength(), " Bold, Italic and light gray color", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    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 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 a2s . co  m*/
    jf.setVisible(true);
}

From source file:SimpleAttributeBoldItalicColor.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Simple Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.LIGHT_GRAY);

    try {/*from  ww w. ja va 2s .  c o  m*/
        document.insertString(document.getLength(), " Bold, Italic and light gray color", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    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) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument doc = new DefaultStyledDocument();
    JTextPane textPane = new JTextPane(doc);
    textPane.setText("this is a test.");

    Random random = new Random();
    for (int i = 0; i < textPane.getDocument().getLength(); i++) {
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set,
                new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        StyleConstants.setFontSize(set, random.nextInt(12) + 12);
        StyleConstants.setBold(set, random.nextBoolean());
        StyleConstants.setItalic(set, random.nextBoolean());
        StyleConstants.setUnderline(set, random.nextBoolean());

        doc.setCharacterAttributes(i, 1, set, true);
    }/*from   w w w  .j a  va 2s . com*/

    frame.add(new JScrollPane(textPane));
    frame.setSize(500, 400);
    frame.setVisible(true);
}

From source file:SimpleAttributeSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Simple Attributes");
    Container content = frame.getContentPane();

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);

    // Insert content
    try {/*from  www .  ja v  a  2 s .c o m*/
        document.insertString(document.getLength(), "Hello Java", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Oops");
    }

    attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.lightGray);

    // Insert content
    try {
        document.insertString(document.getLength(), " - Good-bye Visual Basic", attributes);
    } 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:TabSample.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Tab Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument document = new DefaultStyledDocument();

    //TabStop.ALIGN_CENTER, TabStop.ALIGN_DECIMAL,TabStop.ALIGN_LEFT, TabStop.ALIGN_RIGHT
    //"\tBAR\n", "\tCENTER\n", "\t3.14159265\n", "\tLEFT\n", "\tRIGHT\n" };

    SimpleAttributeSet attributes = new SimpleAttributeSet();

    TabStop tabstop = new TabStop(150, TabStop.ALIGN_BAR, TabStop.LEAD_DOTS);
    int position = document.getLength();
    document.insertString(position, "TabStop.ALIGN_BAR", null);

    TabSet tabset = new TabSet(new TabStop[] { tabstop });
    StyleConstants.setTabSet(attributes, tabset);
    document.setParagraphAttributes(position, 1, attributes, false);

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);//  www  . j  a va2  s.  co m
    JScrollPane scrollPane = new JScrollPane(textPane);
    frame.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 {//from w ww. jav 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) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(new JButton(new AbstractAction("Update") {
        @Override/*w w  w  .  j  a  v a  2  s. c  o  m*/
        public void actionPerformed(ActionEvent ae) {

            StyledDocument doc = (StyledDocument) textPane.getDocument();
            SimpleAttributeSet style = new SimpleAttributeSet();
            StyleConstants.setFontFamily(style, "Serif");
            StyleConstants.setFontSize(style, size++);
            try {
                doc.insertString(doc.getLength(), " one two three", style);
                Dimension d = textPane.getPreferredSize();
                Rectangle r = textPane.modelToView(textPane.getDocument().getLength());
                d.height = r.y + r.height;
                textPane.setPreferredSize(d);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
            frame.pack();
        }

    }));
    frame.add(buttonPanel, BorderLayout.NORTH);
    textPane.setText("this is a test.");
    textPane.setBorder(new LineBorder(Color.BLACK));

    frame.add(new JScrollPane(textPane));
    frame.pack();
    frame.setVisible(true);
}