Encontrar uma palavra dentro do JtextArea?[RESOLVIDO]

2 respostas
dicabeca

to tentando aqui e nao consegui ainda fazer o que encontre e coloque o cursor no texto selecionado dentro do JTextArea alguem pode me ajudar ai ?

2 Respostas

thiago.correa
import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class Class1 extends JFrame {


    JTextArea jTextArea;
    JButton jButton;
    
    public Class1() {
        jTextArea = new JTextArea(20,20);
        jButton = new JButton("Ok");
        
        jButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String texto = jTextArea.getText();
                        
                        Pattern p = Pattern.compile("eu sou o texto");
                        Matcher m = p.matcher(texto);
                        if (m.find()) {
                        System.out.println("opa");
                        System.out.println(m.start());
                            jTextArea.requestFocus();
                            jTextArea.setCaretPosition( m.start() );
                        }
                    }
                });
        
        Container container = getContentPane();
        container.add(jTextArea);
        container.add(jButton);
        this.pack();
        this.setLayout(new FlowLayout());
        this.setVisible(true);        
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Class1();
    }
}
dicabeca

obrigado !!!

Criado 30 de junho de 2009
Ultima resposta 30 de jun. de 2009
Respostas 2
Participantes 2