本文整理匯總了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);
}
}
}