Example usage for javax.swing JTextPane viewToModel

List of usage examples for javax.swing JTextPane viewToModel

Introduction

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

Prototype

@Deprecated(since = "9")
public int viewToModel(Point pt) 

Source Link

Document

Converts the given place in the view coordinate system to the nearest representative location in the model.

Usage

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 a  va 2s . c  o m
            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;
}