本文整理汇总了Java中javax.swing.JTextPane.modelToView方法的典型用法代码示例。如果您正苦于以下问题:Java JTextPane.modelToView方法的具体用法?Java JTextPane.modelToView怎么用?Java JTextPane.modelToView使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.JTextPane
的用法示例。
在下文中一共展示了JTextPane.modelToView方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mouseMoved
import javax.swing.JTextPane; //导入方法依赖的package包/类
@Override
public void mouseMoved(MouseEvent e) {
JTextPane pane = (JTextPane)e.getSource();
StyledDocument doc = pane.getStyledDocument();
int offset = pane.viewToModel(e.getPoint());
Element elem = doc.getCharacterElement(offset);
Highlight h = getHighlight(pane, offset);
Highlight prevHighlight = (Highlight) pane.getClientProperty(PREV_HIGHLIGHT_PROPERTY);
AttributeSet prevAs = (AttributeSet) pane.getClientProperty(PREV_HIGHLIGHT_ATTRIBUTES);
// if(h != null && h.equals(prevHighlight)) {
// return; // nothing to do
// } else
if(prevHighlight != null && prevAs != null) {
doc.setCharacterAttributes(prevHighlight.startOffset, prevHighlight.endOffset - prevHighlight.startOffset, prevAs, true);
pane.putClientProperty(PREV_HIGHLIGHT_PROPERTY, null);
pane.putClientProperty(PREV_HIGHLIGHT_ATTRIBUTES, null);
}
int modifiers = e.getModifiers() | e.getModifiersEx();
if ( (modifiers & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK ||
(modifiers & InputEvent.META_DOWN_MASK) == InputEvent.META_DOWN_MASK)
{
AttributeSet as = elem.getAttributes();
if (StyleConstants.isUnderline(as)) {
// do not underline whats already underlined
return;
}
Font font = doc.getFont(as);
FontMetrics fontMetrics = pane.getFontMetrics(font);
try {
Rectangle rectangle = new Rectangle(
pane.modelToView(elem.getStartOffset()).x,
pane.modelToView(elem.getStartOffset()).y,
fontMetrics.stringWidth(doc.getText(elem.getStartOffset(), elem.getEndOffset() - elem.getStartOffset())),
fontMetrics.getHeight());
if (h != null && offset < elem.getEndOffset() - 1 && rectangle.contains(e.getPoint())) {
Style hlStyle = doc.getStyle("regularBlue-findtype"); // NOI18N
pane.putClientProperty(PREV_HIGHLIGHT_ATTRIBUTES, as.copyAttributes());
doc.setCharacterAttributes(h.startOffset, h.endOffset - h.startOffset, hlStyle, true);
// doc.setCharacterAttributes(h.startOffset, h.endOffset - h.startOffset, as.copyAttributes(), true);
pane.putClientProperty(PREV_HIGHLIGHT_PROPERTY, h);
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
}