Example usage for javax.swing.text BadLocationException printStackTrace

List of usage examples for javax.swing.text BadLocationException printStackTrace

Introduction

In this page you can find the example usage for javax.swing.text BadLocationException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Popup Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {

            int dotPosition = textField.getCaretPosition();
            Rectangle popupLocation = null;
            try {
                popupLocation = textField.modelToView(dotPosition);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }/*from w  w w  .  j a v a 2  s. co  m*/
            System.out.println(popupLocation);
        }
    };

    KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false);
    textField.registerKeyboardAction(actionListener, keystroke, JComponent.WHEN_FOCUSED);

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

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(new JButton(new AbstractAction("Update") {
        @Override/*from  w  w w  .java 2s.  c  o  m*/
        public void actionPerformed(ActionEvent ae) {

            StyledDocument doc = (StyledDocument) textPane.getDocument();
            SimpleAttributeSet style = new SimpleAttributeSet();
            StyleConstants.setFontFamily(style, "Serif");
            StyleConstants.setFontSize(style, size++);
            try {
                doc.insertString(doc.getLength(), " one two three", style);
                Dimension d = textPane.getPreferredSize();
                Rectangle r = textPane.modelToView(textPane.getDocument().getLength());
                d.height = r.y + r.height;
                textPane.setPreferredSize(d);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
            frame.pack();
        }

    }));
    frame.add(buttonPanel, BorderLayout.NORTH);
    textPane.setText("this is a test.");
    textPane.setBorder(new LineBorder(Color.BLACK));

    frame.add(new JScrollPane(textPane));
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void highlight(JTextComponent textComp) {
    try {/*from  w  w w.  j a  va  2s  .  com*/
        Highlighter hilite = textComp.getHighlighter();
        Document doc = textComp.getDocument();
        hilite.addHighlight(3, 5, new MyHighlightPainter(Color.red));
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static int getColumn(int pos, JTextComponent editor) {
    try {//from ww  w. j  av a  2  s. co m
        return pos - Utilities.getRowStart(editor, pos) + 1;
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    return -1;
}

From source file:Main.java

private static void append1(String text) {
    try {/*from   w  ww.  j a  v a  2  s . c o  m*/
        doc1.insertString(doc1.getLength(), text, attribute);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

private static void append2(String text, AttributeSet attributeNew) {
    try {/* www .j a  v  a 2 s .  co m*/
        doc2.insertString(doc2.getLength(), text, attributeNew);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static int getColumn(JTextComponent editor, int pos) {
    try {/*from w w w  . j a v a  2  s .  c  o m*/
        return pos - Utilities.getRowStart(editor, pos) + 1;
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    return -1;
}

From source file:Main.java

/** 
 * Affect a stylised text into an instance of JTextPane. 
 * @param textPane Instance of JTextPane to affect.
 * @param textArray Array of strings./*www.ja va  2 s.  c  o m*/
 * @param styleArray Array of styles.  Must match the textArray.
 */
public static void setText(JTextPane textPane, String[] textArray, String[] styleArray) {
    StyledDocument doc = textPane.getStyledDocument();
    try {
        doc.remove(0, doc.getLength()); // Erase all the previous text.
        for (int i = 0; i < textArray.length; i++) {
            int offset = doc.getLength();
            javax.swing.text.Style style = textPane.getStyle(styleArray[i]);
            doc.insertString(offset, textArray[i], style);
            doc.setParagraphAttributes(offset, textArray[i].length(), style, true);
        }
        textPane.setCaretPosition(0);
    } catch (BadLocationException ignore) {
        ignore.printStackTrace();
    }
}

From source file:Main.java

public static int getRow(JTextComponent editor, int pos) {
    int rn = (pos == 0) ? 1 : 0;
    try {//from ww w.j  a  va  2s .co m
        int offs = pos;
        while (offs > 0) {
            offs = Utilities.getRowStart(editor, offs) - 1;
            rn++;
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    return rn;
}

From source file:Main.java

private static AttributeSet getAttributes(MouseEvent e) {
    JTextPane text = (JTextPane) e.getSource();
    Point mouseLocation = new Point(e.getX(), e.getY());
    int pos = text.viewToModel(mouseLocation);

    if (pos >= 0) {
        try {/*from   w w w . j  av a 2  s. c  om*/
            Rectangle rect = text.modelToView(pos);
            int lowerCorner = rect.y + rect.height;
            if (e.getX() < rect.x && e.getY() < lowerCorner && pos > 0) {
                pos--;
            }
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
        StyledDocument doc = text.getStyledDocument();
        Element element = doc.getCharacterElement(pos);
        return element.getAttributes();
    }
    return null;
}