javax.swing.text.AttributeSet类的使用及代码示例

x33g5p2x  于11个月前 转载在 其他  
字(12.7k)|赞(0)|评价(0)|浏览(37)

本文整理了Java中javax.swing.text.AttributeSet类的一些代码示例,展示了AttributeSet类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AttributeSet类的具体详情如下:
包路径:javax.swing.text.AttributeSet
类名称:AttributeSet

AttributeSet介绍

暂无

代码示例

代码示例来源:origin: SonarSource/sonarqube

Rectangle r = component.modelToView(rowStartOffset);
int lineHeight = fontMetrics.getHeight();
int y = r.y + r.height;
  fonts = new HashMap<>();
 Element root = component.getDocument().getDefaultRootElement();
 int index = root.getElementIndex(rowStartOffset);
 Element line = root.getElement(index);
 for (int i = 0; i < line.getElementCount(); i++) {
  Element child = line.getElement(i);
  AttributeSet as = child.getAttributes();
  String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
  Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
  String key = fontFamily + fontSize;
   fm = component.getFontMetrics(font);
   fonts.put(key, fm);

代码示例来源:origin: pentaho/pentaho-reporting

private HTML.Tag findTag( final AttributeSet attr ) {
 final Enumeration names = attr.getAttributeNames();
 while ( names.hasMoreElements() ) {
  final Object name = names.nextElement();
  final Object o = attr.getAttribute( name );
  if ( o instanceof HTML.Tag ) {
   if ( HTML.Tag.CONTENT == o ) {
    continue;
   }
   if ( HTML.Tag.COMMENT == o ) {
    continue;
   }
   return (HTML.Tag) o;
  }
 }
 return null;
}

代码示例来源:origin: stackoverflow.com

JEditorPane editor = new JEditorPane();
editor.setContentType( "text/html" );
editor.setEditable( false );
editor.read(reader, null);
HTMLDocument doc = (HTMLDocument)editor.getDocument();
ElementIterator it = new ElementIterator(doc);
Element element;
  AttributeSet as = element.getAttributes();
  Enumeration enumm = as.getAttributeNames();
  while( enumm.hasMoreElements() )
    Object name = enumm.nextElement();
    Object value = as.getAttribute( name );
    System.out.println( "\t" + name + " : " + value );

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-kenai-ui

public void run() {
    commChannelsDisplayer.setText(_istr);
    commChannelsDisplayer.validate();
    commChannelsDisplayer.setCaretPosition(0);
    HTMLDocument htm = (HTMLDocument) commChannelsDisplayer.getDocument();
    Element e = htm.getElement(CHAT_BUTTON);
    if (e != null) {
      AttributeSet attr = e.getAttributes();
      Enumeration enu = attr.getAttributeNames();
      while (enu.hasMoreElements()) {
        Object name = enu.nextElement();
        Object value = attr.getAttribute(name);
        if ("model".equals(name.toString())) { //NOI18N
          final DefaultButtonModel model = (DefaultButtonModel) value;
          model.setActionCommand(CHAT_BUTTON);
          model.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              ChatTopComponent ct = ChatTopComponent.findInstance();
              ct.open();
              ct.requestActive();
              ct.setActiveGroup(instProj.getName() + "@muc." + instProj.getKenai().getUrl().getHost()); // NOI18N
            }
          });
        }
      }
    }
  }
});

代码示例来源:origin: stackoverflow.com

JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JEditorPane jep = new JEditorPane("text/html", TEXT);
jep.setEditable(false);
f.add(jep);
f.pack();
f.setVisible(true);
HTMLDocument htmlDoc = (HTMLDocument) jep.getDocument();
System.out.println(htmlDoc.getElement("unique_id"));
ElementIterator iterator = new ElementIterator(htmlDoc);
AttributeSet attrSet = element.getAttributes();
System.out.println(""
  + "Element: '" + element.toString().trim()
  + "', name: '" + element.getName()
  + "', children: " + element.getElementCount()
  + ", attributes: " + attrSet.getAttributeCount()
  + ", leaf: " + element.isLeaf());
Enumeration attrNames = attrSet.getAttributeNames();
while (attrNames.hasMoreElements()) {
  Object attr = attrNames.nextElement();
  System.out.println("  Attribute: '" + attr + "', Value: '"
    + attrSet.getAttribute(attr) + "'");
  Object tag = attrSet.getAttribute(StyleConstants.NameAttribute);
  if (attr == StyleConstants.NameAttribute
    && tag == HTML.Tag.CONTENT) {

代码示例来源:origin: stackoverflow.com

pad.setContentType("text/html");
HTMLEditorKit kit = (HTMLEditorKit)pad.getEditorKit();
HTMLDocument htmldoc = (HTMLDocument)kit.createDefaultDocument();
kit.insertHTML(htmldoc, htmldoc.getLength(), "<p>paragraph <b>1</b></p>", 0, 0, null);
  Element line = doc.getParagraphElement(lastPos+1);
  lastPos = line.getEndOffset();
  XWPFParagraph paragraph = docX.createParagraph();
  for (int elIdx=0; elIdx < line.getElementCount(); elIdx++) {
    final Element frag = line.getElement(elIdx);
    final AttributeSet as = frag.getAttributes();
    final Enumeration<?> ae = as.getAttributeNames();
    while (ae.hasMoreElements()) {
      final Object attrib = ae.nextElement();
        Field f = as.getAttribute(attrib).getClass().getDeclaredField("c");
        f.setAccessible(true);
        Color c = (Color)f.get(as.getAttribute(attrib));
        run.setColor(String.format("%1$02X%2$02X%3$02X", c.getRed(),c.getGreen(),c.getBlue()));
      } else if (CSS.Attribute.FONT_WEIGHT.equals(attrib)) {
        if ("bold".equals(as.getAttribute(attrib).toString())) {
          run.setBold(true);

代码示例来源:origin: stackoverflow.com

HTMLDocument document = (HTMLDocument) messageEditorPaneHTMLEditorKit.createDefaultDocument();
JEditorPane editorPane = new JEditorPane("text/html", "");
editorPane.setEditorKit(messageEditorPaneHTMLEditorKit);
editorPane.setDocument(document);
Enumeration styleAttributes = style.getAttributeNames();
while (styleAttributes.hasMoreElements()) {
  Object attribute = styleAttributes.nextElement();
  String attributeName = attribute.toString();
  Object attributeValue = style.getAttribute(attribute);
  System.out.println(attributeName + ": " + attributeValue);

代码示例来源:origin: org.fudaa.framework.ctulu/ctulu-ui

try {
 writeLock();
 int start = e.getStartOffset();
 DefaultDocumentEvent changes = new DefaultDocumentEvent(start, e.getEndOffset() - start,
   DocumentEvent.EventType.CHANGE);
 AttributeSet sCopy = a.copyAttributes();
 changes.addEdit(new AttributeUndoableEdit(e, sCopy, false));
 MutableAttributeSet attr = (MutableAttributeSet) e.getAttributes();
 Enumeration aNames = attr.getAttributeNames();
 Object value;
 Object aName;
 while (aNames.hasMoreElements()) {
  aName = aNames.nextElement();
  value = attr.getAttribute(aName);
  if (value != null && !value.toString().equalsIgnoreCase(tag.toString())) {

代码示例来源:origin: stackoverflow.com

JEditorPane edPane = new JEditorPane(); 
  edPane.setContentType("text/html");
  System.out.println(edPane.getText());
  edPane.setEditorKit(hek);
  HTMLDocument doc = (HTMLDocument) edPane.getDocument();
  for( int i = 0; i < roots[0].getElementCount(); i++ ) {
    Element element = roots[0].getElement( i );
    if( element.getAttributes().getAttribute( StyleConstants.NameAttribute ) == HTML.Tag.BODY ) {
      body = element;
      break;

代码示例来源:origin: stackoverflow.com

JEditorPane jEditorPaneIsFollower = new JEditorPane();
jEditorPaneIsFollower.setEditable(false);
jEditorPaneIsFollower.setContentType("text/html");
jEditorPaneIsFollower.setDocument(new HTMLDocument());
jEditorPaneIsFollower.setEditorKit(new HTMLEditorKit());
jEditorPaneIsFollower.setText(HTML_TEXT);
ToolTipManager.sharedInstance().registerComponent(jEditorPaneIsFollower);
   Element elem = e.getSourceElement();
   if (elem != null) {
    AttributeSet attr = elem.getAttributes();
    AttributeSet a = (AttributeSet) attr.getAttribute(HTML.Tag.A);
    if (a != null) {
     editor.setToolTipText((String) a.getAttribute(HTML.Attribute.TITLE));

代码示例来源:origin: stackoverflow.com

fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JEditorPane pane = new JEditorPane();
pane.setEditorKit(new NewEditorKit());
pane.setText("test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test ");
StyledDocument doc = (StyledDocument) pane.getDocument();
MutableAttributeSet attr = new SimpleAttributeSet();
attr.addAttribute("strike-color", Color.red);
doc.setCharacterAttributes(0, 9, attr, false);
doc.setCharacterAttributes(10, 19, attr, false);
JScrollPane sp = new JScrollPane(pane);
String kind = elem.getName();
if (kind != null) {
  if (kind.equals(AbstractDocument.ContentElementName)) {
Color c=(Color)getElement().getAttributes().getAttribute("strike-color");
if (c!=null) {
  int y = a.getBounds().y + a.getBounds().height - (int) getGlyphPainter().getDescent(this);

代码示例来源:origin: stackoverflow.com

textPane.setText("Smaple String");
this.add(textPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd();
StyledDocument doc = (StyledDocument) textPane.getDocument();
Element element = doc.getCharacterElement(start);
AttributeSet as = element.getAttributes();
MutableAttributeSet asNew = new SimpleAttributeSet(as.copyAttributes());
StyleConstants.setBold(asNew, !StyleConstants.isBold(as));
doc.setCharacterAttributes(start, textPane.getSelectedText().length(), asNew, true);
String text = (StyleConstants.isBold(as) ? "Cancel Bold" : "Bold");
btnStyle.setText(text);

代码示例来源:origin: stackoverflow.com

pane.setContentType("text/html");
pane.setText("<html>Here is the text with a <a href=\"http://google.com\">link</a></html>");
frm.add(new JScrollPane(pane));
final JButton btn = new JButton(new AbstractAction("Find link") {
    final HTMLDocument doc = (HTMLDocument) pane.getDocument();
    final Collection<String> links = new LinkedHashSet<String>();
      final AttributeSet a = el.getAttributes();
      final AttributeSet anchor = (AttributeSet)a.getAttribute(HTML.Tag.A);
      if (anchor != null) {
        links.add((String)anchor.getAttribute(HTML.Attribute.HREF));

代码示例来源:origin: skylot/jadx

int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
for (int i = 0; i < line.getElementCount(); i++) {
  Element child = line.getElement(i);
  AttributeSet as = child.getAttributes();
  String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
  Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
  String key = fontFamily + fontSize;
  FontMetrics fm = fonts.computeIfAbsent(key, k -> {

代码示例来源:origin: stackoverflow.com

pane.setText("Underline With Different Color");
    StyledDocument doc = (StyledDocument)pane.getDocument();
    MutableAttributeSet attrs = new SimpleAttributeSet();
    attrs.addAttribute("Underline-Color", Color.red);
    doc.setCharacterAttributes(0, doc.getLength()-1, attrs, true);
public View create(Element elem){
  View result = null;
  String kind = elem.getName();
  if(kind != null){
    if(kind.equals(AbstractDocument.ContentElementName)){
  super.paint(g, a);
  Color c = (Color)getElement().getAttributes().getAttribute("Underline-Color");
  if(c != null){
    int y = a.getBounds().y + (int)getGlyphPainter().getAscent(this);

代码示例来源:origin: stackoverflow.com

private final String s1 = String.format(s + "aaaaaaaaaaaaaa<br>", SO, "blue", SO);
private final String s2 = String.format(s + "cccc", SO, "#0000FF", "bbbbbbbbbbb");
private final JEditorPane editor = new JEditorPane(
 "text/html", "<html>" + s1 + s2);
private JComponent makeUI() {
 editor.setEditable(false);
 editor.addHyperlinkListener(new HyperlinkListener() {
  @Override public void hyperlinkUpdate(HyperlinkEvent e) {
   if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
 AttributeSet attrs = element.getAttributes();
 Object o = attrs.getAttribute(HTML.Tag.A);
 if (o instanceof MutableAttributeSet) {
  MutableAttributeSet a = (MutableAttributeSet) o;

代码示例来源:origin: groovy/groovy-core

public int findTabLocation(int offset) {
    // find first {
    boolean cont = true;
    while (offset > -1 && cont) {
      Element el = doc.getCharacterElement(offset);
      Object color =
          el.getAttributes().getAttribute(StyleConstants.Foreground);
      if (!COMMENT_COLOR.equals(color)) {
        cont = segment.array[offset] != '{' &&
            segment.array[offset] != '}';
      }
      offset -= cont ? 1 : 0;
    }
    if (offset > -1 && segment.array[offset] == '{') {
      while (offset > -1 &&
          !Character.isWhitespace(segment.array[offset--])) {
      }
    }
    int index = offset < 0 || segment.array[offset] == '}' ? -4 : 0;
    if (offset > -1) {
      Element top = doc.getDefaultRootElement();
      offset = top.getElement(top.getElementIndex(offset)).getStartOffset();
      while (Character.isWhitespace(segment.array[offset++])) {
        index++;
      }
    }
    return index;
  }
}

代码示例来源:origin: stackoverflow.com

HTMLDocument doc = (HTMLDocument)component.getDocument();
int position = component.getCaretPosition();
Element e = doc.getCharacterElement( position );
AttributeSet as = e.getAttributes();
AttributeSet anchor = (AttributeSet)as.getAttribute(HTML.Tag.A);
    Rectangle r = component.modelToView(position);

代码示例来源:origin: net.imagej/imagej-ui-swing

private ActionListener getAction(final MouseEvent event) {
  Element e = document.getCharacterElement(textPane.viewToModel(event.getPoint()));
  ActionListener action = (ActionListener)e.getAttributes().getAttribute(ACTION_ATTRIBUTE);
  return action;
}

代码示例来源:origin: stackoverflow.com

JEditorPane editorPane = new JEditorPane() {

  @Override
  public String getToolTipText(MouseEvent evt) {
    String text = null;
    int pos = viewToModel(evt.getPoint());
    if (pos >= 0) {
      HTMLDocument hdoc = (HTMLDocument) getDocument();
      javax.swing.text.Element e = hdoc.getCharacterElement(pos);
      AttributeSet a = e.getAttributes();

      SimpleAttributeSet value = (SimpleAttributeSet) a.getAttribute(HTML.Tag.A);
      if (value != null) {
        String href = (String) value.getAttribute(HTML.Attribute.HREF);
        if (href != null) {
          text = href;
        }
      }
    }
    return text;
  }

};

相关文章