突出显示JTextArea上的文本

时间:2016-03-05 18:38:47

标签: java loops highlight

我需要在JTextArea上突出显示文本,使用JTextField获取用户输入,然后使用Button来获取JTextFields文本。最终我需要做到这一点,它突出了JTextArea exp中所有出现的String:我有200个单词和10次出现的单词' dog'我希望狗被突出10次。

我似乎遇到了循环本身的问题,它突出显示第一次出现,然后没有出现。

    private void getSearch() throws BadLocationException {

                 // my textfield (searchT)
               String jtf = searchT.getText();

                // my jtextarea  (userField);
                String jta = userField.getText();

                hilit = userField.getHighlighter();
                hilit.removeAllHighlights();


         int index = jta.indexOf(jtf);

        while(index >=0){
        System.out.println("looping");

       int len = jtf.length();
        hilit.addHighlight(index,index+len, painter);
        index = jta.indexOf(jta, index+len);


    }

     }   




**PER REQUEST:**


public class project extend Frame implements ActionListener{

   Highlighter hilit;
   Highlighter.HighlightPainter painter;



public project(){


     userField = new JTextArea();
     searchT = new JTextField("Spotlight");
     searchT.setEditable(true);
     searchT.setBorder(new LineBorder (Color.black));

     searchB = new JButton("Search");

        hilit = new DefaultHighlighter();
        painter = new                    DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);





// JButton (searchB) with ActionListener 
 searchB.addActionListener(new java.awt.event.ActionListener(){
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        try {
            searchBActionPerformed(evt);
        } catch (BadLocationException ex) {
            Logger.getLogger(DaWord.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

           private void searchBActionPerformed(ActionEvent evt) throws BadLocationException  {
               Object sb = evt.getSource();
             if(searchB==sb){
           getSearch();

               }

           }
           private void getSearch() throws BadLocationException {

             // my jtextfield
           String jtf = searchT.getText();
            // my jtextarea
            String jta = userField.getText();

            hilit = userField.getHighlighter();
            hilit.removeAllHighlights();


     int index = jta.indexOf(jtf);

    while(index >=0){
    System.out.println("looping");


    System.out.println("im searching for"+index);
    int len = jtf.length();
    hilit.addHighlight(index,index+len, painter);
    index = jta.indexOf(jta, index+len);
    System.out.println("this is what I want to highlight"+ index);
    // remove hightlights with a timer for example after 10 seconds
}

 }   

1 个答案:

答案 0 :(得分:1)

是的,请搜索正确的字符串:

while(index >=0){
  System.out.println("looping");
  int len = jtf.length();
  hilit.addHighlight(index,index+len, painter);
  index = jta.indexOf(jtf, index+len);
}

-

我根据你的代码编写了一个小程序:唯一的问题就是我说你正在寻找错误的字符串(你没有在indexOf()中将jta改为jtf。否则它可以正常工作并用红色突出聚光灯(我的颜色你可以把它设置成你想要的颜色:

class project extends Frame { // implements ActionListener{

 Highlighter hilit;
 Highlighter.HighlightPainter painter;

JTextArea userField;
JTextField searchT;
JButton searchB;

public project(){


 userField = new JTextArea("spotlight aaa spotlight");
 searchT = new JTextField("spotlight");
 searchT.setEditable(true);
//     searchT.setBorder(new LineBorder (Color.black));

 searchB = new JButton("Search");

    hilit = new DefaultHighlighter();
    painter = new                    DefaultHighlighter.DefaultHighlightPainter(Color.red);





// JButton (searchB) with ActionListener 
 searchB.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt) {
    try {
        searchBActionPerformed(evt);
    } catch (BadLocationException ex) {
//            Logger.getLogger(DaWord.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

       private void searchBActionPerformed(ActionEvent evt) throws BadLocationException  {
           Object sb = evt.getSource();
         if(searchB==sb){
       getSearch();

           }

       }
       private void getSearch() throws BadLocationException {

         // my jtextfield
       String jtf = searchT.getText();
        // my jtextarea
        String jta = userField.getText();

        hilit = userField.getHighlighter();
        hilit.removeAllHighlights();


 int index = jta.indexOf(jtf);

while(index >=0){
System.out.println("looping");


System.out.println("im searching for"+jtf+" "+index);
int len = jtf.length();
hilit.addHighlight(index,index+len, painter);
index = jta.indexOf(jtf, index+len);
System.out.println("this is what I want to highlight"+ index);
// remove hightlights with a timer for example after 10 seconds
}

}

});

setSize(500, 500);

setLayout(new FlowLayout());

add(userField);
add(searchT);
add(searchB);

setVisible(true);

}

}