سؤال

So i've got another horrible error on Java. I've got this code:

class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
    public MyHighlightPainter(Color color) {
        super(color);
    }   

Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.yellow);

public void highlightt(JTextArea textComp, String pattern) {
    try {
        Highlighter hilite = textComp.getHighlighter();
        Document doc = textComp.getDocument();
        String text = doc.getText(0, doc.getLength());
        int pos = 0;

        while((pos=text.toUpperCase().indexOf(pattern.toUpperCase(),pos))>=0) {

            hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
            pos +=pattern.length();
        }
    } catch(Exception e) {
        e.printStackTrace();
    }   
}

} 

search.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            highlightt(field, word.getText());
        }
    });

And On highlightt(field, word.getText()); error that undefined for the type new ActionListener

Can anyone help me? Thanks :).

هل كانت مفيدة؟

المحلول

Just keep the method definition and instance declaration inside class -

do this

 class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
        public MyHighlightPainter(Color color) {
            super(color);
        }   

    Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.yellow);

    public void highlightt(JTextArea textComp, String pattern) {
        try {
            Highlighter hilite = textComp.getHighlighter();
            Document doc = textComp.getDocument();
            String text = doc.getText(0, doc.getLength());
            int pos = 0;

            while((pos=text.toUpperCase().indexOf(pattern.toUpperCase(),pos))>=0) {

                hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
                pos +=pattern.length();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }   
    }

} // close class
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top