How to disable / enable buttons in one JFrame depending on the option from another JFrame?

0

I currently have two buttons in a JFrame : one to save and the other to update. In other JFrame , I have to register and edit.

When I click on the register, I want the save to be on and update off, and when to edit, vice versa. I know I have to use meuJbutton.setEnabled(true/false) methods, but I do not know how and where to formulate the conditional to activate / deactivate the buttons. I tried something like

The 2 jFrames are as follows:

Main Screen:

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;
    }                       
}

Signing up:

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 did not work. I'm out of ideas. I'm using Netbeans own tools.

    
asked by anonymous 17.10.2017 / 16:11

1 answer

2

Your approach is not very good because in addition to creating multiple JFrames (which in most cases is not necessary, just JDialogs would already fit 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 class Register, you will not be able to recover the value of it.

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 Registration 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.
}
    
17.10.2017 / 16:39