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

private void initComponents() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane textPane = new JTextPane();
    ((AbstractDocument) textPane.getDocument()).addDocumentListener(new DocumentListener() {
        @Override//from   w w w. j av  a2 s . co m
        public void insertUpdate(final DocumentEvent de) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        StyledDocument doc = (StyledDocument) de.getDocument();
                        int start = Utilities.getRowStart(textPane, Math.max(0, de.getOffset() - 1));
                        int end = Utilities.getWordStart(textPane, de.getOffset() + de.getLength());

                        String text = doc.getText(start, end - start);

                        for (String emoticon : imageTokens) {
                            int i = text.indexOf(emoticon);
                            while (i >= 0) {
                                final SimpleAttributeSet attrs = new SimpleAttributeSet(
                                        doc.getCharacterElement(start + i).getAttributes());
                                if (StyleConstants.getIcon(attrs) == null) {
                                    switch (emoticon) {
                                    case imageToken:
                                        StyleConstants.setIcon(attrs, anImage);
                                        break;
                                    }
                                    doc.remove(start + i, emoticon.length());
                                    doc.insertString(start + i, emoticon, attrs);
                                }
                                i = text.indexOf(emoticon, i + emoticon.length());
                            }
                        }
                    } catch (BadLocationException ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }
    });

    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(300, 300));
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);
    try {/*from  ww  w  . jav  a  2 s.  c  o m*/
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                System.out.println("set");
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Object o = UIManager.get("TextArea[Enabled+NotInScrollPane].borderPainter");

    UIDefaults paneDefaults = new UIDefaults();
    paneDefaults.put("TextPane.borderPainter", o);

    JTextPane pane = new JTextPane();
    pane.setMargin(new Insets(10, 10, 10, 10));

    pane.putClientProperty("Nimbus.Overrides", paneDefaults);
    pane.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
    pane.setText("this \nis \na \ntest\n");
    add(pane);

}

From source file:Main.java

public Main() {
    setSize(400, 400);/*ww w  .jav  a  2 s  .  c  o m*/
    styleSheet.addRule(".someclass1 {color: blue;}");
    styleSheet.addRule(".someclass2 {color: green;}");

    htmlEditorKit.setStyleSheet(styleSheet);
    htmlDocument = (HTMLDocument) htmlEditorKit.createDefaultDocument();
    JTextPane jTextPane = new JTextPane();
    jTextPane.setEditorKit(htmlEditorKit);
    jTextPane.setDocument(htmlDocument);

    try {
        Element htmlElement = htmlDocument.getRootElements()[0];
        bodyElement = htmlElement.getElement(0);

        Container contentPane = getContentPane();
        contentPane.add(jTextPane, BorderLayout.CENTER);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addContent("<span class=someclass1>test 1</span><br>");
        addContent("<span class=someclass2>test 2</span><br>");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public MainClass() {
    JFrame frame = new JFrame();
    JTextPane textPane = new JTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane);

    JPanel north = new JPanel();

    JMenuBar menu = new JMenuBar();
    JMenu styleMenu = new JMenu();
    styleMenu.setText("Style");

    Action boldAction = new BoldAction();
    boldAction.putValue(Action.NAME, "Bold");
    styleMenu.add(boldAction);//from www . ja  v a 2 s .c  o m

    Action italicAction = new ItalicAction();
    italicAction.putValue(Action.NAME, "Italic");
    styleMenu.add(italicAction);

    Action foregroundAction = new ForegroundAction();
    foregroundAction.putValue(Action.NAME, "Color");
    styleMenu.add(foregroundAction);

    Action formatTextAction = new FontAndSizeAction();
    formatTextAction.putValue(Action.NAME, "Font and Size");
    styleMenu.add(formatTextAction);

    menu.add(styleMenu);

    north.add(menu);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(north, BorderLayout.NORTH);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(800, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    textField.setText("Hit Enter to Add Text to Text Pane");
    getContentPane().add(textField, BorderLayout.NORTH);
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Document doc = textPane.getDocument();
                doc.insertString(doc.getLength(), " " + textField.getText(), null);
                textField.setText("");//clear
                Dimension d = textPane.getPreferredSize();
                Rectangle r = textPane.modelToView(textPane.getDocument().getLength());
                d.height = r.y + r.height;
                textPane.setPreferredSize(d);
                getContentPane().validate();
            } catch (Exception e2) {
            }//w  w  w  .j  ava 2  s.co m
        }
    });

    JPanel south = new JPanel();
    textPane = new JTextPane();
    textPane.setText("Some \ntext");
    textPane.setEditable(false);
    textPane.setPreferredSize(new Dimension(120, 23));

    south.add(textPane);
    getContentPane().add(south, BorderLayout.SOUTH);
}

From source file:MainClass.java

public MainClass() {
    JFrame frame = new JFrame();
    textPane = new JTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane);

    JPanel north = new JPanel();
    JButton print = new JButton("Print");
    print.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            paintToPDF(textPane);//from w  w  w . ja  va 2s  . c om
        }
    });
    JMenuBar menu = new JMenuBar();
    JMenu styleMenu = new JMenu();
    styleMenu.setText("Style");

    Action boldAction = new BoldAction();
    boldAction.putValue(Action.NAME, "Bold");
    styleMenu.add(boldAction);

    Action italicAction = new ItalicAction();
    italicAction.putValue(Action.NAME, "Italic");
    styleMenu.add(italicAction);

    menu.add(styleMenu);

    north.add(menu);
    north.add(print);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(north, BorderLayout.NORTH);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(800, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:au.org.ala.delta.ui.TextFileViewer.java

public TextFileViewer(Window owner, File file) throws IOException {
    super(owner);
    setModal(true);//from w w  w .  java  2 s  . com
    getContentPane().setLayout(new BorderLayout());
    viewer = new JTextPane();

    viewer.setEditable(false);
    viewer.setBackground(Color.WHITE);
    viewer.setBorder(null);
    viewer.setOpaque(true);
    viewer.setFont(UIManager.getFont("Label.font"));

    JScrollPane scroller = new JScrollPane(viewer);
    getContentPane().add(scroller, BorderLayout.CENTER);

    setTitle(file.getAbsolutePath());
    displayFileContents(file);
    pack();
}

From source file:hr.fer.zemris.vhdllab.view.LogHistoryView.java

@Override
protected JComponent createControl() {
    JTextPane textPane = new JTextPane();
    textPane.setEditable(false);//from  w w  w. j av  a  2s . c o m
    textPane.setAutoscrolls(true);

    setupLogAppender(textPane);

    return new JScrollPane(textPane);
}

From source file:net.itransformers.topologyviewer.rightclick.impl.CLIReportViewer.java

public <G> void handleRightClick(JFrame parent, String v, Map<String, String> graphMLParams,
        Map<String, String> rightClickParams, File projectPath, File versionDir) throws Exception {
    Logger logger = Logger.getLogger(CLIReportViewer.class);

    JFrame frame = new JFrame(" report for " + v + " ");
    frame.setSize(600, 400);/*from w  ww.  ja  va2 s .com*/
    frame.getContentPane().setLayout(new BorderLayout());
    JTextPane text = new JTextPane();
    text.setEditable(true);
    text.setContentType("text/html");
    String postDiscoveryFolderPath = rightClickParams.get("post-discovery-file-path");
    String reportFileName = rightClickParams.get("reportFileName");
    logger.info("CLI report executed in " + versionDir + " for " + File.separator + postDiscoveryFolderPath
            + File.separator + v + File.separator + reportFileName);

    File xmlReport = new File(versionDir.getAbsolutePath() + File.separator + postDiscoveryFolderPath
            + File.separator + v + File.separator + reportFileName);
    if (xmlReport.exists()) {
        text.setText(FileUtils.readFileToString(xmlReport));
        JScrollPane scrollPane = new JScrollPane(text);
        frame.getContentPane().add("Center", scrollPane);
        frame.setVisible(true);

    } else {
        JOptionPane.showMessageDialog(parent, "Report does not exist! Please generate it first!");
    }
}

From source file:UndoExample5.java

public UndoExample5() {
    super("Undo/Redo Example 5");

    pane = new JTextPane();
    pane.setEditable(true); // Editable
    getContentPane().add(new JScrollPane(pane), BorderLayout.CENTER);

    // Add a menu bar
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);/*from www.  ja va 2  s. co m*/

    // Populate the menu bar
    createMenuBar();

    // Create the undo manager and actions
    MonitorableUndoManager manager = new MonitorableUndoManager();
    pane.getDocument().addUndoableEditListener(manager);

    Action undoAction = new UndoAction(manager);
    Action redoAction = new RedoAction(manager);

    // Add the actions to buttons
    JPanel panel = new JPanel();
    final JButton undoButton = new JButton("Undo");
    final JButton redoButton = new JButton("Redo");
    undoButton.addActionListener(undoAction);
    redoButton.addActionListener(redoAction);

    undoButton.setEnabled(false);
    redoButton.setEnabled(false);
    panel.add(undoButton);
    panel.add(redoButton);
    getContentPane().add(panel, BorderLayout.SOUTH);

    // Assign the actions to keys
    pane.registerKeyboardAction(undoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK),
            JComponent.WHEN_FOCUSED);
    pane.registerKeyboardAction(redoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK),
            JComponent.WHEN_FOCUSED);

    // Handle events from the MonitorableUndoManager
    manager.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            MonitorableUndoManager m = (MonitorableUndoManager) evt.getSource();
            boolean canUndo = m.canUndo();
            boolean canRedo = m.canRedo();

            undoButton.setEnabled(canUndo);
            redoButton.setEnabled(canRedo);

            undoButton.setToolTipText(canUndo ? m.getUndoPresentationName() : null);
            redoButton.setToolTipText(canRedo ? m.getRedoPresentationName() : null);
        }
    });
}