Example usage for javax.swing.text Document insertString

List of usage examples for javax.swing.text Document insertString

Introduction

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

Prototype

public void insertString(int offset, String str, AttributeSet a) throws BadLocationException;

Source Link

Document

Inserts a string of content.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent textComp = new JTextField("Initial Text");
    Document doc = textComp.getDocument();

    // Append some text
    doc.insertString(doc.getLength(), "some text", null);
}

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);//from  w  ww . ja  v  a2 s  . c  o  m
    jf.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent textComp = new JTextField("Initial Text");
    Document doc = textComp.getDocument();

    // Insert some text after the 5th character
    int pos = 5;//w w w  .  j a  v  a2  s  .  co m
    doc.insertString(pos, "some text", null);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent textComp = new JTextField("Initial Text");
    Document doc = textComp.getDocument();

    // Replace the first 3 characters with some text
    int pos = 0;/*  w  ww  .  ja  va 2  s.c o m*/
    int len = 3;
    doc.remove(pos, len);
    doc.insertString(pos, "new text", null);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextArea comp = new JTextArea();
    String actionName = "Lowercase";

    JFrame f = new JFrame();
    f.add(new JScrollPane(comp));
    f.setSize(300, 300);/*from w w w  .  ja v  a2s  .com*/
    f.setVisible(true);
    comp.getInputMap().put(KeyStroke.getKeyStroke("F2"), actionName);

    comp.getActionMap().put(actionName, new TextAction(actionName) {
        public void actionPerformed(ActionEvent evt) {
            JTextComponent comp = getTextComponent(evt);

            if (comp.getSelectionStart() == comp.getSelectionEnd()) {
                if (comp.getCaretPosition() < comp.getDocument().getLength()) {
                    try {
                        int pos = comp.getCaretPosition();
                        Document doc = comp.getDocument();
                        String str = doc.getText(pos, 1).toLowerCase();

                        doc.remove(pos, 1);
                        doc.insertString(pos, str, null);
                        comp.moveCaretPosition(pos + 1);
                    } catch (Exception e) {
                        System.out.println();
                    }
                }
            } else {
                int s = comp.getSelectionStart();
                int e = comp.getSelectionEnd();

                comp.replaceSelection(comp.getSelectedText().toLowerCase());
                comp.select(s, e);
            }
        }
    });
}

From source file:MainClass.java

protected void append(String s, AttributeSet attributes) {
    Document d = textPane.getDocument();
    try {/*from w  w  w  . ja v a2 s . c  om*/
        d.insertString(d.getLength(), s, attributes);
    } catch (BadLocationException ble) {
    }
}

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) {
            }//from  ww w  . j  a v  a2s .  c om
        }
    });

    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:Main.java

public Main() {
    editorPane.setDocument(doc);/*  w  w  w . j  a va 2 s .c om*/
    editorPane.setEditorKit(styledEditorKit);
    JScrollPane scrollpane = new JScrollPane(editorPane);
    scrollpane.setPreferredSize(new Dimension(500, 400));
    JPanel comboPanel = new JPanel();
    comboPanel.add(fontBox);

    setLayout(new BorderLayout());
    add(scrollpane, BorderLayout.CENTER);
    add(comboPanel, BorderLayout.SOUTH);

    Document doc = editorPane.getDocument();
    for (int i = 0; i < 20; i++) {
        int offset = doc.getLength();
        String str = "This is line number: " + i + "\n";
        try {
            doc.insertString(offset, str, null);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

    fontBox.addActionListener(e -> {
        int size = (Integer) fontBox.getSelectedItem();
        Action fontAction = new StyledEditorKit.FontSizeAction(String.valueOf(size), size);
        fontAction.actionPerformed(e);
    });
}

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

private void setupLogAppender(final JTextPane textPane) {
    Logger.getRootLogger().addAppender(new AppenderSkeleton() {
        @Override/*from   ww w  .  j a  va2 s . com*/
        protected void append(LoggingEvent event) {
            if (!event.getLevel().equals(Level.INFO)) {
                return;
            }

            StringBuilder sb = new StringBuilder();
            String time = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(event.timeStamp);
            sb.append(time).append("  ").append(event.getMessage());

            Document document = textPane.getDocument();
            int documentLength = document.getLength();
            try {
                document.insertString(documentLength, sb.toString() + "\n", null);
            } catch (BadLocationException e) {
                throw new IllegalStateException(e);
            }
            // scroll to end of document
            textPane.setCaretPosition(documentLength);
        }

        @Override
        public boolean requiresLayout() {
            return false;
        }

        @Override
        public void close() {
        }
    });
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JTextComponent comp = getTextComponent(e);
    if (comp == null)
        return;/*  www .  j  a va2s .c  o m*/
    Document doc = comp.getDocument();
    int start = comp.getSelectionStart();
    int end = comp.getSelectionEnd();
    try {
        int left = Utilities.getWordStart(comp, start);
        int right = Utilities.getWordEnd(comp, end);
        String word = doc.getText(left, right - left);
        doc.remove(left, right - left);
        doc.insertString(left, word.toUpperCase(), null);
        comp.setSelectionStart(start);
        comp.setSelectionEnd(end);
    } catch (Exception ble) {
        return;
    }
}