Example usage for javax.swing.text StyleConstants setAlignment

List of usage examples for javax.swing.text StyleConstants setAlignment

Introduction

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

Prototype

public static void setAlignment(MutableAttributeSet a, int align) 

Source Link

Document

Sets alignment.

Usage

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   www . java2s  .  c  o m*/
        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:JTextPaneStyle.java

public static void main(String args[]) {
    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);

    try {//from  w  w w.  j a  v  a2 s  . c  om
        document.insertString(document.getLength(), message, style);
    } 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:TextPaneSample.java

public static void main(String args[]) {
    String title = (args.length == 0 ? "TextPane Example" : args[0]);
    JFrame frame = new JFrame(title);
    Container content = frame.getContentPane();

    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);

    // Insert content
    try {//w w  w .  j a va 2 s  . com
        document.insertString(document.getLength(), message, style);
    } catch (BadLocationException badLocationException) {
        System.err.println("Oops");
    }

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    StyleConstants.setBold(attributes, true);
    StyleConstants.setItalic(attributes, true);

    // Insert content
    try {
        document.insertString(document.getLength(), "Hello Java", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Oops");
    }

    // 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);

    // Insert content
    try {
        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);
    content.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

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 {//from  w ww  .j  a  v  a 2  s  .co  m
        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:Main.java

public Main() throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(edit);//from w w w. java  2  s. com
    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);
}

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);
}

From source file:TextSamplerDemo.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("small", regular);
    StyleConstants.setFontSize(s, 10);

    s = doc.addStyle("large", regular);
    StyleConstants.setFontSize(s, 16);

    s = doc.addStyle("icon", regular);
    StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
    ImageIcon pigIcon = createImageIcon("images/Pig.gif", "a cute pig");
    if (pigIcon != null) {
        StyleConstants.setIcon(s, pigIcon);
    }/*from  w w w  .  j a v a 2  s. c om*/

    s = doc.addStyle("button", regular);
    StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
    ImageIcon soundIcon = createImageIcon("images/sound.gif", "sound icon");
    JButton button = new JButton();
    if (soundIcon != null) {
        button.setIcon(soundIcon);
    } else {
        button.setText("BEEP");
    }
    button.setCursor(Cursor.getDefaultCursor());
    button.setMargin(new Insets(0, 0, 0, 0));
    button.setActionCommand(buttonString);
    button.addActionListener(this);
    StyleConstants.setComponent(s, button);
}

From source file:dylemator.DylematorUI.java

private void displayCenteredText(String text) {
    textArea.setText("");
    text = "\n\n\n" + text;
    SimpleAttributeSet attribs = new SimpleAttributeSet();
    if (sd == null)
        sd = new SettingsDialog(this, true);
    float fnt = sd.getFontSize();
    StyledDocument doc = (StyledDocument) textArea.getDocument();
    Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);//doc.addStyle("MyStyle",null);
    StyleConstants.setFontSize(style, (int) fnt);
    StyleConstants.setLineSpacing(attribs, 0.5f);
    StyleConstants.setFontFamily(style, "Segoe UI");
    StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_CENTER);
    textArea.setParagraphAttributes(attribs, true);

    try {//from  w w w. jav  a 2  s.co  m
        doc.insertString(doc.getLength(), text, style);
        doc.setParagraphAttributes(0, doc.getLength() - 1, attribs, false);
    } catch (BadLocationException ex) {
        Logger.getLogger(DylematorUI.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:ome.formats.importer.gui.GuiCommonElements.java

/**
 * Add a text pane to the parent container
 * // ww w  .jav  a 2 s .c  o m
 * @param container - parent container
 * @param text - text for new Text Pane
 * @param placement - TableLayout placement within the parent container
 * @param debug - turn on/off red debug borders
 * @return new JTextPane
 */
public static synchronized JTextPane addTextPane(Container container, String text, String placement,
        boolean debug) {
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);

    try {
        document.insertString(document.getLength(), text, null);
    } catch (BadLocationException e) {
        log.error("BadLocationException inserting text to document.");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);
    textPane.setEditable(false);
    textPane.setFocusable(false);
    container.add(textPane, placement);

    if (debug == true)
        textPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red),
                textPane.getBorder()));

    return textPane;
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Formats the text and displays it in a {@link JTextPane}.
 * /*  w w w. jav  a 2 s . c  om*/
 * @param text          The text to display.
 * @param foreground   The foreground color.
 * @return See above.
 */
public static JTextPane buildTextPane(String text, Color foreground) {
    if (text == null)
        text = "";
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
    if (foreground != null)
        StyleConstants.setForeground(style, foreground);
    try {
        document.insertString(document.getLength(), text, style);
    } catch (BadLocationException e) {
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);
    textPane.setEditable(false);
    textPane.setFocusable(false);
    return textPane;
}