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.
Please access the link and provide a [mcve] so that it is possible to test the problem.
– user28595
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
– user28595
I just edited it. And I understood what you meant about calling the variable, but how could I approach it in a better way?
– Luiz Felipe Falchi Barreto
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.
– UzumakiArtanis