Logica Error(I think)

Asked

Viewed 42 times

0

Good evening programmers, I’m having a problem with my code and I don’t understand where it’s wrong.

I created the following jFrame and the buttons "<" and ">" should, after the entries are made, navigate between the registered items. But only the last and second-to-last values are displayed. Someone can help me?

jFrame

Part 1:

public class TelaAcessorio extends javax.swing.JFrame {

/**
 * Creates new form TelaAcessorio
 */
public TelaAcessorio() {
    initComponents();
    // desativar botoes
    jBAntes.setEnabled(false);
    jBNext.setEnabled(false);


}
//intanciação
 Acessorio aces = new Acessorio();
// vetor acessorio 
 Acessorio[] vet = new Acessorio[5];
 int i =0;


 private void jBGravarActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    // verifica 
    if(i<=4){

    aces.setCodigo(jTCod.getText());
    aces.setDesc(jTDesc.getText());
    aces.setPreco(jTPreco.getText());

    //add obj no vetor
    vet[i]=aces;
    i++;
    limpar();

    }

    else{

        jBNext.setEnabled(true);
        jTCod.setEditable(false);
        jTDesc.setEditable(false);
        jTPreco.setEditable(false);
        jBGravar.setEnabled(false);
        i=0;

    }

}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
      TelaPrincipal tela = new TelaPrincipal();
      tela.setVisible(true);
      this.dispose();
}                                        

private void jBNextActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:

  // if(i==1){
    //   jBNext.setEnabled(true);
   //}


   aces = vet[i];

   jTCod.setText(aces.getCodigo());
   jTDesc.setText(aces.getDesc());
   jTPreco.setText(aces.getPreco());

   i++;

    if(i==5){
       i=4;
   }

}                                      

private void jBAntesActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:

    if(i==3){
        jBAntes.setEnabled(true);
    }

   aces = vet[i];

   jTCod.setText(aces.getCodigo());
   jTDesc.setText(aces.getDesc());
   jTPreco.setText(aces.getPreco());

   i--;

   if(i==0){
       jBAntes.setEnabled(false);

    }

}                                       

private void limpar(){
     //limpar
    jTCod.setText(null);
    jTDesc.setText(null);
    jTPreco.setText(null);
    // foco
    jTCod.requestFocus();
}

is all part of the same class Accessories, only cut because of the hidden part of the code it is unnecessary to show here.

I believe there’s a mistake in logic I just didn’t understand which

  • 2

    As it comes to swing, could provide a [mcve] of your code, so it is possible to run and simulate the problem?

  • Yeah, thanks for the tip. I’ll fix that

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.