JFrame para pesquisa não atualiza Janela Principal

1 resposta
slompo

Bem hoje fiquei na duvida em qual seria a melhor área para se postar o meu problema, Basico ou Avançado, então acabei postando nas duas, peço desculpas por isso :oops:

Olá pessoal, estudando o Core Java 2 Fundamentos I, fiz alguns exercícios e com minha curiosidade resolvi tentar implementar uma nova janela de pesquisa ao invés de ser no mesmo JFrame Principal segue logo abaixo o código:

Executa.java

public class Executa {

    public static void main(String[] args) {
        JFrame frame = new FindTextInTextArea();
        frame.setVisible(true);
    }
}

FindTextInTextArea.java

public class FindTextInTextArea extends JFrame implements ActionListener {

    private JButton findButton, replaceButton, jbFR;
    private JTextArea textArea;
    private JTextField from, to;

    public FindTextInTextArea() {
        setTitle("Encontrado Texto no JTextArea");
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(d.width, d.height-30);
        setIconImage(Toolkit.getDefaultToolkit().getImage("D:/icon.png"));

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });

        textArea = new JTextArea(8,40);
        getContentPane().add(textArea,"Center");

        JPanel p = new JPanel();
        jbFR = new JButton("F&R");
        jbFR.addActionListener(this);
        p.add(jbFR);
        findButton = new JButton("Find");
        findButton.addActionListener(this);
        p.add(findButton);
        replaceButton = new JButton("Replace");
        replaceButton.addActionListener(this);
        p.add(replaceButton);
        p.add(new JLabel("From", JLabel.CENTER));
        from = new JTextField("",10);
        from.addActionListener(this);
        p.add(from);
        p.add(new JLabel("To", JLabel.CENTER));
        to = new JTextField("",10);
        to.addActionListener(this);
        p.add(to);
        getContentPane().add(p, "South");
    }

    public void actionPerformed(ActionEvent e) {
        Object s = e.getSource();
        //A posição do texto é selecionada embora não apareça a seleção no JTextArea
        if(s==findButton) {
            String f = from.getText();
            int n = textArea.getText().indexOf(f);
            textArea.select(n, n+f.length());
            String sel = textArea.getSelectedText();
            System.out.println(sel);
        } else if(s==replaceButton){
            String f = from.getText();
            int n = textArea.getText().indexOf(f);
            if(n>=0&&f.length()>0){
                textArea.replaceRange(to.getText(), n, n+f.length());
            }
        } else if(s==jbFR) {
            JFrame a = new JFrameFindReplace(textArea);
            a.setVisible(true);
        }
    }
}

JFrameFindReplace.java

public class JFrameFindReplace extends JFrame implements ActionListener {
    
    private JTextArea jtaText;

    private JTextField jtfFrom, jtfTo;
    private JButton jbFind, jbReplace;
    
    public JFrameFindReplace(JTextArea textArea) {
        this.jtaText = textArea;
        initComponents();
    }
    
    public void initComponents(){
        setTitle("Find and Replace");
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        double x = d.getHeight()/2;
        double y = d.getWidth()/2;
        double defaultX = 105/2;
        double defaultY = 360/2;
        setBounds((int) (y - defaultY), (int) (x - defaultX), 360, 105);
        JPanel panel = new JPanel();
        panel.add(new JLabel("From",JLabel.CENTER));
        jtfFrom = new JTextField("", 21);
        panel.add(jtfFrom);
        jbFind = new JButton("Find");
        jbFind.addActionListener(this);
        panel.add(jbFind);
        panel.add(new JLabel("To",JLabel.CENTER));
        jtfTo = new JTextField("", 20);
        panel.add(jtfTo);
        jbReplace = new JButton("Replace");
        jbReplace.addActionListener(this);
        panel.add(jbReplace);
        getContentPane().add(panel);
    }

    public void actionPerformed(ActionEvent e) {
        Object s = e.getSource();
        if(s==jbFind) {
            String f = jtfFrom.getText();
            int n = jtaText.getText().indexOf(f);
            jtaText.select(n, n+f.length());
            jtaText.repaint();
        } else if(s==jbReplace){
            String f = jtfFrom.getText();
            int n = jtaText.getText().indexOf(f);
            if(n>=0&&f.length()>0){
                jtaText.replaceRange(f, n, n+f.length());
                jtaText.repaint();
            }
        }
    }
}

Bom Pessoal o problema é que ao digitar no campo FROM e TO e apos clicka em REPLACE ele buscaria no JTextArea do JFrame principal e substituiria, no entanto isto não acontece…

Desde já muito obrigado a todos =D

1 Resposta

ViniGodoy

Oi, na dúvida, poste em qualquer uma, e deixe que a moderação mova o tópico.

Mas jamais duplique tópicos. Isso pode levar alguém a perder tempo respondendo uma dúvida já solucionada no outro tópico. Também força vc a responder duas vezes as mesmas dúvidas.

Esse é um tópico da área de interface gráfica.

Vou trancar esse, quem quiser acompanhar:
http://www.guj.com.br/posts/list/149093.java

Criado 12 de janeiro de 2010
Ultima resposta 12 de jan. de 2010
Respostas 1
Participantes 2