2011-11-16 107 views
2

我正在尋找一種快速方法在JTextPane中查找字符串並更改其中的樣式,以便突出顯示。我現在有是這樣的(tpOutput是JTextPane的問題,strSearch要搜索的字符串..杜):在JTextPane中查找字符串的偏移量

int index = tpOutput.getText().indexOf(strSearch); 
StyledDocument doc = tpOutput.getStyledDocument(); 
doc.setCharacterAttributes(i, strSearch.length(), doc.getStyle("exampleStyle") , false); 

然而,美如那將是如果它的工作,它計算錯了換行符,所以如果我在

foobarTTT 
abcd123 
abcd123 

搜索文本「foobar的」會正確地突出顯示第一行的「FOOBAR」。然而,在

abcd123 
abcd123 
foobarTTT 

會(如果它們存在和下面2個空格)突出「obarTT」

我可能做這件事錯了,試圖讓只使用文本偏移容易。任何人都知道這樣做的正確方法?

+0

它如何循環?上面的代碼看起來像會找到第一個發生的東西 –

+0

你是對的。爲了我的目的,只有第一次發生是重要的。我說錯了這個例子,對不起。 – Zymox

回答

2

IndexOutOfBounds也許是從doc.getStyle("exampleStyle"),通過使用MutableAttributeSet作品對我來說,

可以通過使用此SSCCE

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.text.*; 

public class TextPaneAttributes extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public TextPaneAttributes() { 
     final JTextPane textPane = new JTextPane(); 
     StyledDocument doc = textPane.getStyledDocument(); 
     final MutableAttributeSet standard = new SimpleAttributeSet(); 
     StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER); 
     doc.setParagraphAttributes(0, 0, standard, true); 
     MutableAttributeSet keyWord = new SimpleAttributeSet(); 
     StyleConstants.setForeground(keyWord, Color.red); 
     StyleConstants.setItalic(keyWord, true); 
     textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n"); 
     doc.setCharacterAttributes(0, 3, keyWord, false); 
     doc.setCharacterAttributes(19, 4, keyWord, false); 
     try { 
      doc.insertString(0, "Start of text\n", null); 
      doc.insertString(doc.getLength(), "End of text\n", keyWord); 
     } catch (Exception e) { 
     } 
     final MutableAttributeSet selWord = new SimpleAttributeSet(); 
     StyleConstants.setForeground(selWord, Color.blue); 
     StyleConstants.setItalic(selWord, true); 
     JScrollPane scrollPane = new JScrollPane(textPane); 
     scrollPane.setPreferredSize(new Dimension(200, 200)); 
     add(scrollPane); 

     JToggleButton toggleButton = new JToggleButton("where is 'four'"); 
     toggleButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent actionEvent) { 
       //AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); 
       //boolean selected = abstractButton.getModel().isSelected(); 
       int index = textPane.getText().indexOf("four"); 
       StyledDocument doc = textPane.getStyledDocument(); 
       doc.setCharacterAttributes(index, 4, selWord, false); 
      } 
     }); 
     add(toggleButton, BorderLayout.SOUTH); 
    } 

    public static void main(String[] args) { 
     Runnable doRun = new Runnable() { 

      @Override 
      public void run() { 
       TextPaneAttributes frame = new TextPaneAttributes(); 
       frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(doRun); 
    } 
} 
2

我的猜測:也許有什麼換行符在的JTextPane和StyledDocument中使用的差異,讓我們說的JTextPane使用\n和StyledDocument中的使用\r\n

+1

+1,是的,使用JTextPane時可能會出現問題。有關更多信息和解決方案,請參見[文本和新行](http://tips4java.wordpress.com/2009/02/07/text-and-new-lines/)。 – camickr

1

我發現同樣的事情,我用這個代碼調整:

private int countCarrierReturns(StringBuilder sb, int end) { 
     final String cr = "\r"; 
     int lines = 0; 
     int init = sb.indexOf(cr, 0); 

     if (init == -1) { 
      return 0; 
     } 

     while (init < end) { 
      lines++; 

      init = sb.indexOf(cr, init + 1); 
     } 

     return lines; 
    } 

'int end'是我第一個應用風格的字符。