Example usage for javax.swing JTextPane JTextPane

List of usage examples for javax.swing JTextPane JTextPane

Introduction

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

Prototype

public JTextPane() 

Source Link

Document

Creates a new JTextPane.

Usage

From source file:Main.java

public static void main(String[] args) {
    JTextPane text = new JTextPane() {
        @Override/*from   ww  w . j ava2  s .  c  o  m*/
        public String getToolTipText() {
            return ((JComponent) getParent()).getToolTipText();
        }

        @Override
        public String getToolTipText(MouseEvent event) {
            return ((JComponent) getParent()).getToolTipText(event);
        }
    };
    text.setText("Lorem ipsum dolor sit");
    ToolTipManager.sharedInstance().registerComponent(text);

    JFrame frame = new JFrame("Testing");
    JPanel panel = new JPanel(new BorderLayout());
    panel.setToolTipText("tooltip from parent");
    frame.setContentPane(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(text);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextPane textPane = new JTextPane();

    DefaultStyledDocument doc = (DefaultStyledDocument) textPane.getDocument();
    Enumeration e1 = doc.getStyleNames();
    while (e1.hasMoreElements()) {
        String styleName = (String) e1.nextElement();
        System.out.println(styleName);
        Style style = doc.getStyle(styleName);
        int count = style.getAttributeCount();
        System.out.println(count);

        Enumeration e = style.getAttributeNames();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            if (o instanceof String) {
                String attrName = (String) o;
                Object attrValue = style.getAttribute(attrName);
                System.out.println(attrValue);
            } else if (o == StyleConstants.NameAttribute) {
                styleName = (String) style.getAttribute(o);
                System.out.println(styleName);
            } else if (o == StyleConstants.ResolveAttribute) {
                Style parent = (Style) style.getAttribute(o);
                System.out.println(parent.getName());
            } else {
                String attrName = o.toString();
                System.out.println(attrName);
                Object attrValue = style.getAttribute(o);
                System.out.println(attrValue);
            }//from  w ww  . ja v  a  2  s  .c o  m
        }
    }
}

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(/*from  w  ww  .ja va2  s  . co m*/
                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[] argv) throws Exception {
    JTextComponent textcomp = new JTextPane();
    textcomp.setText("Initial Text");
    textcomp.getDocument().addDocumentListener(new DocumentListener() {
        public void insertUpdate(DocumentEvent evt) {
            int off = evt.getOffset();
            System.out.println("off:" + off);
            int len = evt.getLength();
            System.out.println("len:" + len);
            try {
                String str = evt.getDocument().getText(off, len);
                System.out.println(str);
            } catch (BadLocationException e) {
            }/*from w w  w . j ava  2 s.c  om*/
        }

        public void removeUpdate(DocumentEvent evt) {
            int off = evt.getOffset();
            System.out.println("off:" + off);
            int len = evt.getLength();
            System.out.println("len:" + len);
        }

        public void changedUpdate(DocumentEvent evt) {
            int off = evt.getOffset();
            System.out.println("off:" + off);
            int len = evt.getLength();
            System.out.println("len:" + len);
        }
    });
}

From source file:Main.java

public static void main(String[] args) {
    final JTextPane tp = new JTextPane();
    JButton withFocus = new JButton("Select with focus");
    tp.addMouseListener(new MouseAdapter() {

        @Override/*from w  ww.j a  v  a 2  s . co m*/
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
                tp.selectAll();
            }
        }

    });

    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(tp));
    frame.pack();
    frame.setLocationRelativeTo(null);
    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);//from   w  w w  .  ja  va 2  s  . c  o m
    d.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);//from w w w .ja va2s .co  m
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 100; i++) {
        sb.append("this is a test. ");
    }/*  w w w .  ja v a 2s. c  o m*/
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextPane newsTextPane = new JTextPane();
    newsTextPane.setContentType("text/html");
    newsTextPane.setEditable(false);
    newsTextPane.setText(sb.toString());

    JScrollPane scrollPane = new JScrollPane(newsTextPane);
    scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    frame.add(scrollPane);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String HTMLTEXT = "<html><head><style>.foot{color:red} .head{color:black}</style></head>"
            + "<span style='font-family:consolas'>java2s.com</span><br/>"
            + "<span style='font-family:tahoma'>java2s.com</span>";
    JTextPane textPane1 = new JTextPane();

    textPane1.setContentType("text/html");
    textPane1.setText(HTMLTEXT);/*from   w ww .  j a v  a 2s. c  om*/

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(textPane1));
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

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

    JTextPane editorPane = new JTextPane();
    editorPane.setSelectedTextColor(Color.red);

    // set content as html
    // editorPane.setContentType("text/html");
    editorPane.setText("<p color='#FF0000'>Cool!</p>");

    // added <u></u> to underlone button
    JButton label = new JButton("button");

    label.setAlignmentY(0.85f);/* w  w  w.  j av  a 2  s . c  o  m*/

    label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {

            if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
                JOptionPane.showMessageDialog(null, "Hello!");
            }
        }
    });

    editorPane.insertComponent(label);
    frame.getContentPane().add(editorPane);
    frame.pack();
    frame.setVisible(true);
}