Example usage for javax.swing JTextPane getStyledDocument

List of usage examples for javax.swing JTextPane getStyledDocument

Introduction

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

Prototype

public StyledDocument getStyledDocument() 

Source Link

Document

Fetches the model associated with the editor.

Usage

From source file:TextSamplerDemo.java

private JTextPane createTextPane() {
    String[] initString = { "This is an editable JTextPane, ", //regular
            "another ", //italic
            "styled ", //bold
            "text ", //small
            "component, ", //large
            "which supports embedded components..." + newline, //regular
            " " + newline, //button
            "...and embedded icons..." + newline, //regular
            " ", //icon
            newline + "JTextPane is a subclass of JEditorPane that "
                    + "uses a StyledEditorKit and StyledDocument, and provides "
                    + "cover methods for interacting with those objects." };

    String[] initStyles = { "regular", "italic", "bold", "small", "large", "regular", "button", "regular",
            "icon", "regular" };

    JTextPane textPane = new JTextPane();
    StyledDocument doc = textPane.getStyledDocument();
    addStylesToDocument(doc);//from www  .  j ava 2s.co  m

    try {
        for (int i = 0; i < initString.length; i++) {
            doc.insertString(doc.getLength(), initString[i], doc.getStyle(initStyles[i]));
        }
    } catch (BadLocationException ble) {
        System.err.println("Couldn't insert initial text into text pane.");
    }

    return textPane;
}

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

public JTextPane createJTextPane(String text) {
    JTextPane jtp = new JTextPane();
    jtp.setText(text);//from  w w w . j  a va2 s  .co  m
    SimpleAttributeSet underline = new SimpleAttributeSet();
    StyleConstants.setUnderline(underline, true);
    jtp.getStyledDocument().setCharacterAttributes(0, text.length(), underline, true);
    jtp.setEditable(false);
    jtp.setOpaque(false);
    jtp.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10));
    jtp.setBorder(BorderFactory.createEmptyBorder());
    jtp.setForeground(Color.blue);
    jtp.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

    return jtp;
}

From source file:org.drugis.addis.gui.wizard.AddStudyWizard.java

private static JComponent buildTip(String tip) {
    JTextPane area = new JTextPane();
    StyledDocument doc = area.getStyledDocument();
    addStylesToDoc(doc);/*from  www.j  ava  2s. c om*/

    area.setBackground(new Color(255, 180, 180));

    try {
        doc.insertString(0, "x", doc.getStyle("tip"));
        doc.insertString(doc.getLength(), " Tip: \n", doc.getStyle("bold"));
        doc.insertString(doc.getLength(), tip, doc.getStyle("regular"));
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    area.setEditable(false);

    JScrollPane pane = new JScrollPane(area);
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    pane.setPreferredSize(TextComponentFactory.textPaneDimension(area, 270, 70));

    pane.setWheelScrollingEnabled(true);
    pane.getVerticalScrollBar().setValue(0);

    return pane;
}

From source file:org.geopublishing.atlasViewer.GpCoreUtil.java

public static void setTabs(final JTextPane textPane, final int charactersPerTab) {
    final FontMetrics fm = textPane.getFontMetrics(textPane.getFont());
    final int charWidth = fm.charWidth('w');
    final int tabWidth = charWidth * charactersPerTab;

    final TabStop[] tabs = new TabStop[10];

    for (int j = 0; j < tabs.length; j++) {
        final int tab = j + 1;
        tabs[j] = new TabStop(tab * tabWidth);
    }/*from www . ja  v a  2s  . c om*/

    final TabSet tabSet = new TabSet(tabs);
    final SimpleAttributeSet attributes = new SimpleAttributeSet();
    StyleConstants.setTabSet(attributes, tabSet);
    final int length = textPane.getDocument().getLength();
    textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
}

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

/**
 * Builds the UI component displaying the exception.
 * /*from  w  w w . ja v  a2 s  . co m*/
 * @return See above.
 */
private JTextPane buildExceptionArea() {
    JTextPane pane = UIUtilities.buildExceptionArea();
    StyledDocument document = pane.getStyledDocument();
    Style style = pane.getLogicalStyle();
    //Get the full debug text
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    exception.printStackTrace(pw);
    try {
        document.insertString(document.getLength(), sw.toString(), style);
    } catch (BadLocationException e) {
    }

    return pane;
}

From source file:org.optaplanner.benchmark.impl.aggregator.swingui.BenchmarkAggregatorFrame.java

private JComponent createNoPlannerFoundTextField() {
    String infoMessage = "No planner benchmarks have been found in the benchmarkDirectory ("
            + benchmarkAggregator.getBenchmarkDirectory() + ").";
    JTextPane textPane = new JTextPane();

    textPane.setEditable(false);/* ww  w .  j  ava 2s .  com*/
    textPane.setText(infoMessage);

    // center info message
    StyledDocument styledDocument = textPane.getStyledDocument();
    SimpleAttributeSet center = new SimpleAttributeSet();
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
    StyleConstants.setBold(center, true);
    styledDocument.setParagraphAttributes(0, styledDocument.getLength(), center, false);
    return textPane;
}

From source file:org.quackbot.gui.GUIConsoleAppender.java

public void init() {
    //Init styles
    for (JTextPane curPane : ImmutableList.of(gui.BerrorLog, gui.CerrorLog)) {
        StyledDocument doc = curPane.getStyledDocument();
        doc.addStyle("Normal", null);
        StyleConstants.setForeground(doc.addStyle("Class", null), Color.blue);
        StyleConstants.setForeground(doc.addStyle("Error", null), Color.red);
        //BotSend gets a better shade of orange than Color.organ gives
        StyleConstants.setForeground(doc.addStyle("BotSend", null), new Color(255, 127, 0));
        //BotRecv gets a better shade of green than Color.green gives
        StyleConstants.setForeground(doc.addStyle("BotRecv", null), new Color(0, 159, 107));
        StyleConstants.setBold(doc.addStyle("Server", null), true);
        StyleConstants.setItalic(doc.addStyle("Thread", null), true);
        StyleConstants.setItalic(doc.addStyle("Level", null), true);
    }//  w  w w  .  j  av  a  2s.  com

    log.debug("Inited GUILogAppender, processing any saved messages");
    synchronized (initMessageQueue) {
        inited = true;
        while (!initMessageQueue.isEmpty())
            append(initMessageQueue.poll());
    }
}

From source file:org.quackbot.gui.GUIConsoleAppender.java

/**
 * Used by Log4j to write something from the LoggingEvent. This simply points to
 * WriteOutput which writes to the the GUI or to the console
 * @param event//ww w  . jav a2s . c  o  m
 */
@Override
public void append(final ILoggingEvent event) {
    if (!inited)
        synchronized (initMessageQueue) {
            if (!inited) {
                initMessageQueue.add(event);
                return;
            }
        }

    //Grab bot info off of MDC
    final String botId = MDC.get("pircbotx.id");
    final String botServer = MDC.get("pircbotx.server");
    final String botPort = MDC.get("pircbotx.port");

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                //Figure out where this is going
                JTextPane textPane = (StringUtils.isBlank(botId)) ? gui.CerrorLog : gui.BerrorLog;
                JScrollPane scrollPane = (StringUtils.isBlank(botId)) ? gui.CerrorScroll : gui.BerrorScroll;

                //Configure stle
                StyledDocument doc = textPane.getStyledDocument();
                Style msgStyle = event.getLevel().isGreaterOrEqual(Level.WARN) ? doc.getStyle("Error")
                        : doc.getStyle("Normal");

                doc.insertString(doc.getLength(), "\n", doc.getStyle("Normal"));
                int prevLength = doc.getLength();
                doc.insertString(doc.getLength(), "[" + dateFormatter.format(event.getTimeStamp()) + "] ",
                        doc.getStyle("Normal")); //time
                //doc.insertString(doc.getLength(), "["+event.getThreadName()+"] ", doc.getStyle("Thread")); //thread name
                doc.insertString(doc.getLength(), event.getLevel().toString() + " ", doc.getStyle("Level")); //Logging level
                doc.insertString(doc.getLength(), event.getLoggerName() + " ", doc.getStyle("Class"));
                if (StringUtils.isNotBlank(botId)) {
                    String port = !botPort.equals("6667") ? ":" + botPort : "";
                    doc.insertString(doc.getLength(), "<" + botId + ":" + botServer + port + "> ",
                            doc.getStyle("Server"));
                }
                doc.insertString(doc.getLength(), messageLayout.doLayout(event).trim(), msgStyle);

                //Only autoscroll if the scrollbar is at the bottom
                //JScrollBar scrollBar = scroll.getVerticalScrollBar();
                //if (scrollBar.getVisibleAmount() != scrollBar.getMaximum() && scrollBar.getValue() + scrollBar.getVisibleAmount() == scrollBar.getMaximum())
                textPane.setCaretPosition(prevLength);
            } catch (Exception e) {
                addError("Exception encountered when logging", e);
            }
        }
    });
}

From source file:org.smart.migrate.ui.ImportThread.java

public ImportThread(ImportManager importManager, UIView uIView, JTextPane logger, JProgressBar progressBar,
        MigratePlan migratePlan) {/*from   ww w  .jav a2s  .  c  o  m*/
    this.importManager = importManager;
    this.logger = logger;
    this.progressBar = progressBar;

    this.migratePlan = migratePlan;
    this.uIView = uIView;
    importManager.setImportLogger(this);
    addStylesToDocument(logger.getStyledDocument());
}

From source file:pl.otros.logview.gui.actions.search.SearchAction.java

private void scrollToSearchResult(ArrayList<String> toHighlight, JTextPane textPane) {
    if (toHighlight.size() == 0) {
        return;/* w  ww  .j ava2  s.com*/
    }
    try {
        StyledDocument logDetailsDocument = textPane.getStyledDocument();
        String text = logDetailsDocument.getText(0, logDetailsDocument.getLength());
        String string = toHighlight.get(0);
        textPane.setCaretPosition(Math.max(text.indexOf(string), 0));
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}