2009-03-11 1 views

답변

3

이 나는 ​​스타일 문서와는 HTMLEditorKit의 도움의 비트를 사용하고 싶었던 것을 알아 냈다. 나는 형광펜을 완전히 잊었다. 불행히도 내가하고 싶었던 것은 전경을 변경하는 것이지, 배경이 아니라, 형광펜을 사용하는 것이 훨씬 더 어려웠습니다. 그러나 그것은 올바른 길로 나를 보내 셨습니다. 감사합니다.
+0

#highlightHyperlink에서 덮어 쓴 실제 색상을 수정하고 #removeHyperlinkHighlight에서 기억 된 색상을 사용하는 것이 좋습니다. – ordnungswidrig

+0

이 예제에서는 더 좋을 것입니다. –

2

Highlighting Words in a JTextComponent을 참조하십시오.

public class HighlightHyperlinkExample { 
    private static Element lastHyperlinkElementEntered; 
    private static JEditorPane textPane; 


    public static void main(String[] args) { 
     textPane = new JEditorPane(); 
     textPane.setContentType(new HTMLEditorKit().getContentType()); 
     JScrollPane scrollPane = new JScrollPane(textPane); 
     textPane.setText(
       "Sample text with <a href=\"x\">a link</a> and another <a href=\"x\">link</a>."); 

     initListeners(); 

     JFrame frame = new JFrame(); 
     frame.add(scrollPane); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 


    private static void initListeners() { 
     textPane.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseExited(MouseEvent e) { 
       removeHyperlinkHighlight(); 
      } 
     }); 
     textPane.addMouseMotionListener(new MouseMotionListener() { 
      public void mouseDragged(MouseEvent e) { 
      } 

      public void mouseMoved(MouseEvent e) { 
       Point pt = new Point(e.getX(), e.getY()); 
       int pos = textPane.viewToModel(pt); 
       if (pos >= 0) { 
        HTMLDocument hdoc = (HTMLDocument) textPane.getDocument(); 
        Element elem = hdoc.getCharacterElement(pos); 
        if (elem != null) { 
         AttributeSet a = elem.getAttributes(); 
         AttributeSet anchor = (AttributeSet) a.getAttribute(HTML.Tag.A); 
         if (anchor != null) { 
          //only highlight anchor tags 
          highlightHyperlink(elem); 
         } else { 
          removeHyperlinkHighlight(); 
         } 
        } 
       } 
      } 
     }); 
    } 

    private static void removeHyperlinkHighlight() { 
     changeColor(lastHyperlinkElementEntered, Color.BLUE); 
     lastHyperlinkElementEntered = null; 
    } 

    private static void highlightHyperlink(Element hyperlinkElement) { 
     if (hyperlinkElement != lastHyperlinkElementEntered) { 
      lastHyperlinkElementEntered = hyperlinkElement; 
      changeColor(hyperlinkElement, Color.RED); 
     } 
    } 

    private static void changeColor(Element el, Color color) { 
     if (lastHyperlinkElementEntered != null) { 
      HTMLDocument doc = (HTMLDocument) textPane.getDocument(); 
      int start = el.getStartOffset(); 
      int end = el.getEndOffset(); 
      StyleContext ss = doc.getStyleSheet(); 
      Style style = ss.addStyle("HighlightedHyperlink", null); 
      style.addAttribute(StyleConstants.Foreground, color); 
      doc.setCharacterAttributes(start, end - start, style, false); 
     } 
    } 
} 
+0

감사 :

JTextArea textComp = new JTextArea(); // Highlight the occurrences of the word "public" highlight(textComp, "public"); // Creates highlights around all occurrences of pattern in textComp public void highlight(JTextComponent textComp, String pattern) { // First remove all old highlights removeHighlights(textComp); try { Highlighter hilite = textComp.getHighlighter(); Document doc = textComp.getDocument(); String text = doc.getText(0, doc.getLength()); int pos = 0; // Search for pattern while ((pos = text.indexOf(pattern, pos)) >= 0) { // Create highlighter using private painter and apply around pattern hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter); pos += pattern.length(); } } catch (BadLocationException e) { } } // Removes only our private highlights public void removeHighlights(JTextComponent textComp) { Highlighter hilite = textComp.getHighlighter(); Highlighter.Highlight[] hilites = hilite.getHighlights(); for (int i=0; i<hilites.length; i++) { if (hilites[i].getPainter() instanceof MyHighlightPainter) { hilite.removeHighlight(hilites[i]); } } } // An instance of the private subclass of the default highlight painter Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red); // A private subclass of the default highlight painter class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter { public MyHighlightPainter(Color color) { super(color); } } 

+0

[JTextComponent에서 단어 강조 표시] 링크를 열면 바이러스가있는 페이지로 연결됩니다. – Tdorno