Retornar valor ao JFrame Principal

Boa tarde a todos,

Estou desenvolvendo um programa para meu aprendizado, porem travai nessa parte.

Meu projeto tem um formulário principal e uma JTable e tem um botão que abre a tela de consultar

Nessa tela de consultar tbm tem uma JTable e um campo de pesquisar, ao selecionar uma linha da tabela ela teria que fechar e retornar o índice da tabela para o JFrame Principal, eu estou fazendo dessa forma na tela de Consulta

public class Consultar extends javax.swing.JFrame {
    
    /**
     * Creates new form Consultar
     */
    public Consultar() {
        initComponents();
        c.preencherTabela(tblConsulta);      
    }
    //Outros Codigos
   private void tblConsultaMouseClicked(java.awt.event.MouseEvent evt) {                                         
        index = tblConsulta.getSelectedRow();
        p.recebeIndex(index);
        dispose();
    }  
    //Outros Codigos
    private Principal p;
    int index;
    String Nome;
    Cliente cliente = new Cliente();
    ClienteDAO c = new ClienteDAO();

Agora no Form principal:

public class Principal extends javax.swing.JFrame {

    /**
     * Creates new form Principal
     */
    public Principal() {
        initComponents();
        c.preencherTabela(tblCliente);
    }
    //Outros Codigos

  //Abre a tela de consulta
  private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {                                             
        Consultar tela = new Consultar();
        tela.setVisible(true);
    }
    private void getCliente(Cliente cliente){
        //Cliente cliente = new Cliente();
        txtNome.setText(cliente.getNome());
        txtCEP.setText(""+cliente.getCep());
        txtEndereco.setText(cliente.getEndereco());
        txtNumero.setText(""+cliente.getEndnumero());
        txtTelefone.setText(""+cliente.getTelefone());
        txtCelular.setText(""+cliente.getCelular());
    }
    //Outros Codigos
    public void recebeIndex(int index){
        JOptionPane.showMessageDialog(null, index);
        MoveTabela.selectAndScroll(tblCliente, index);
        Nome = "" + tblCliente.getValueAt(tblCliente.getSelectedRow(),0);
        CEP = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),1).toString());
        Endereco = "" + tblCliente.getValueAt(tblCliente.getSelectedRow(),2);
        Endnumero = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),3).toString());
        Telefone = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),4).toString());
        Celular = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),5).toString());
        cliente = new Cliente(Nome,CEP,Endereco,Endnumero,Telefone,Celular);
        getCliente(cliente);
    }
    String Nome;
    String NomeAntigo;
    int CEP;
    String Endereco;
    int Endnumero;
    int Telefone;
    int Celular;
    boolean alterar = false;
    ClienteDAO c = new ClienteDAO();
    Cliente cliente = new Cliente();

O JOptionPane exibe a mensagem com o índice certinho, porem abaixo disso nada mais funciona.

Aonde estou errando?

Atenciosamente,
Rafael

Primeiramente, queres retornar o que da tela de consulta? Apenas o índice?

Sim, apenas o índice da linha selecionada, isso eu pego dessa maneira.

index = tblConsulta.getSelectedRow();  

Agora quero fechar a tela de consultar e passar ele para a tela principal

Essa função recebe o index e seleciona na tabela principal o indice que foi retornado na tela de consulta.

public void recebeIndex(int index){ JOptionPane.showMessageDialog(null, index); MoveTabela.selectAndScroll(tblCliente, index); Nome = "" + tblCliente.getValueAt(tblCliente.getSelectedRow(),0); CEP = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),1).toString()); Endereco = "" + tblCliente.getValueAt(tblCliente.getSelectedRow(),2); Endnumero = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),3).toString()); Telefone = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),4).toString()); Celular = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),5).toString()); cliente = new Cliente(Nome,CEP,Endereco,Endnumero,Telefone,Celular); getCliente(cliente); }

porem, não esta funcionando, ele executa tudo certinho, ate ali no JOptionPane (aparece o valor do índice), porem o resto não vai.

Essa parte recebe o índice e seleciona a linha na tabela cliente.

MoveTabela.selectAndScroll(tblCliente, index);  

tente:

tbCliente.changeSelection(index, index, false, false);

Quando você pega o cep, telefone, endereço, retorna tudo certinho?

[quote=bomba544]tente:

tbCliente.changeSelection(index, index, false, false);

Quando você pega o cep, telefone, endereço, retorna tudo certinho?[/quote]
Tbm não.

Não deu para testar dessa forma, pq agora esta dando um erro de Exception em p.recebeIndex(index)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Telas.Consultar.tblConsultaMouseClicked(Consultar.java:117) at Telas.Consultar.access$300(Consultar.java:15) at Telas.Consultar$2.mouseClicked(Consultar.java:76) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6508) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

EDIT:
Eu resolvi parcialmente com esse codigo

private void tblConsultaMouseClicked(java.awt.event.MouseEvent evt) { index = tblConsulta.getSelectedRow(); if(p == null){ p = new Principal(); p.setVisible(true); p.recebeIndex(index); }else{ p.setVisible(true); p.recebeIndex(index); } dispose(); }

Porem agora sempre abre a janela principal varias vezes, ex:

Abro a tela de consulto e seleciono uma linha, ele abre uma nova tela principal, porem com o dados mostrando, se clico novamente esse mesmo clico fica se repetindo abrindo a principal varias vezes.

EDIT2
Eu resolvi depois que vi esse video

Fiz da seguinte forma, tela Principal

[code]public class Principal extends javax.swing.JFrame {

/**
 * Creates new form Principal
 */
public Principal() {
    initComponents();
    c.preencherTabela(tblCliente);
}

private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {                                             
    Consultar tela = new Consultar();
    if (tela==null){
        tela = new Consultar();
        tela.setVisible(true);
    }else{
        tela.setVisible(true);
    }
    tela.recebeTela(this);        
} 
public void recebeIndex(int index){
    //JOptionPane.showMessageDialog(null, index);
    //tblCliente.changeSelection(index, index, false, false);
    MoveTabela.selectAndScroll(tblCliente, index);
    Nome = "" + tblCliente.getValueAt(tblCliente.getSelectedRow(),0);
    CEP = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),1).toString());
    Endereco = "" + tblCliente.getValueAt(tblCliente.getSelectedRow(),2);
    Endnumero = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),3).toString());
    Telefone = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),4).toString());
    Celular = Integer.parseInt(tblCliente.getValueAt(tblCliente.getSelectedRow(),5).toString());
    cliente = new Cliente(Nome,CEP,Endereco,Endnumero,Telefone,Celular);
    getCliente(cliente);
}[/code]

Tela de Consulta

[code]public class Consultar extends javax.swing.JFrame {

/**
 * Creates new form Consultar
 */

public Consultar() {
    initComponents();
    c.preencherTabela(tblConsulta);      
}

private void tblConsultaMouseClicked(java.awt.event.MouseEvent evt) {                                         
    index = tblConsulta.getSelectedRow();
    if(p != null){
        p.recebeIndex(index);
        this.dispose();
    }
} 

public void recebeTela(Principal prin){
    txtNome.setText(Nome);
    this.p = prin;
}
private Principal p;
int index;
String Nome;
Cliente cliente = new Cliente();
ClienteDAO c = new ClienteDAO();[/code]

So que não entendi porque na tela principal tenho que passar ela mesmo por paramentro através do método tela.recebeTela(this), poderia me explicar.

Obrigado