How to disable/enable buttons of a Jframe as an option coming from another Jframe?

Asked

Viewed 834 times

0

Currently I have two buttons on one JFrame: one to save and the other to update. Another JFrame, I have to register and edit.

When I click Sign Up, I want the Save to be turned on and update off, and when editing, vice versa. I know I have to use the methods meuJbutton.setEnabled(true/false), but I don’t know how and where to formulate the conditional to enable/disable the buttons. I tried something like

The 2 jFrames follow:

Telaprincipal:

public class TelaPrincipal extends javax.swing.JFrame {

    public int salvar;

        public TelaPrincipal() {
        initComponents();
        this.setResizable(false);
        this.setLocationRelativeTo(null);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new Cadastro().setVisible(true);
        dispose();
        salvar = 1;
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new Cadastro().setVisible(true);
        dispose();
        salvar = 0;
    }                       
}

Register:

public class Cadastro extends javax.swing.JFrame {

    TelaPrincipal t = new TelaPrincipal();

    public Cadastro() {       
        this.setResizable(false);
        initComponents();
        this.setLocationRelativeTo(null);
        if (t.salvar == 1){
            jButton1.setEnabled(true);
            jButton3.setEnabled(false);}
        else{
            jButton1.setEnabled(false);
            jButton3.setEnabled(true);
        } //no caso o botão 1 sempre está desligado e o 3 ligado não importa o valor de salvar.
    }

But it didn’t work. I’m out of ideas. I’m using the Netbeans tools themselves.

  • 1

    Please access the link and provide a [mcve] so that it is possible to test the problem.

  • 1

    Your approach is bad, you are validating something on a second screen accessing variable from the first. Provide an example as above to suggest a better approach. Another thing, avoid using more than one Jframe, prefer to always use Jdialogs

  • I just edited it. And I understood what you meant about calling the variable, but how could I approach it in a better way?

  • From what you’re saying, it seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

1 answer

2


Your approach is not very good, as well as creating multiple Jframes (which in most cases is not necessary, only JDialogs already serve for this case), you are still creating a completely new instance of the main screen to validate something defined in another instance. The variable salvar is instance, creating a new object in the Register class, you will not be able to recover its value.

A better approach is to create public constants, and from them, validate the buttons, passing as argument to the class Cadastro:

public class TelaPrincipal extends javax.swing.JFrame {

     public TelaPrincipal() {
        initComponents();
        this.setResizable(false);
        this.setLocationRelativeTo(null);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new Cadastro(Cadastro.CADASTRAR).setVisible(true);
        dispose();
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new Cadastro(Cadastro.EDITAR).setVisible(true);
        dispose();
    }                       
}

And in the Register class:

 public class Cadastro extends javax.swing.JFrame {

    public static final int EDITAR = 0;
    public static final int CADASTRAR = 1;

    public Cadastro(int tipo)  {       
        this.setResizable(false);
        initComponents();
        this.setLocationRelativeTo(null);

            jButton1.setEnabled(tipo == CADASTRAR);
            jButton3.setEnabled(tipo == EDITAR);}

        } //no caso o botão 1 sempre está desligado e o 3 ligado não importa o valor de salvar.
}
  • Thank you so much for the teaching, it was all right. I did not know that I could arrive by this means, and I will keep in mind about the jDialogs that you told me.

Browser other questions tagged

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