We are carrying out a "Maintainer" program in the classroom to manage a database using MySQL and Java NetBeans. We are just fine, but we want to know how to create some sort of event to toggle "Update Button" disabled while inserting/modifying info into database via textbox/clicked.
When executing the project, we can modify data directly to the Database at MySQL by writing/modifying into table's selected/clicked text box; after modification is done, we need to press ENTER so modifications gets written, then we press the Modificar button to make the update effective.
What we want to know is how we can toggle the Modificar button unclickable while editing on textbox active. Because if we don't press ENTER first after modification is done, clicking on Update button while textbox is active/editying throws an error due to data isn't effective yet.
Here we can edit information by clicking on text box (*red frame), while doing that (while textbox clicked and editing info) we want that "Update Button" gets Disabled/Unclickable (green circle).
So far here is our code to the event, but is not what we want: (where "btnactualizar" its Update Button and "tabla_cliente" is the table that contains the textbox)
private void tabla_clienteKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER){
btnactualizar.setEnabled(true);
}
}
This is the Update Button:
private void btnactualizarActionPerformed(java.awt.event.ActionEvent evt) {
String Rut = String.valueOf(tabla_cliente.getValueAt(tabla_cliente.getSelectedRow(), 0));
String Nombre = String.valueOf(tabla_cliente.getValueAt(tabla_cliente.getSelectedRow(), 1));
String Apellido = String.valueOf(tabla_cliente.getValueAt(tabla_cliente.getSelectedRow(), 2));
String Patente = String.valueOf(tabla_cliente.getValueAt(tabla_cliente.getSelectedRow(), 3));
int conf = JOptionPane.showConfirmDialog(null, "Estas seguro de actualizar? ", "mensaje", JOptionPane.YES_NO_OPTION, JOptionPane.YES_NO_OPTION);
if (conf == JOptionPane.OK_OPTION) {
exeCliente.actualizar_Cliente(Rut, Nombre, Apellido, Patente);
}
}
This is the Update Method:
public void actualizar_Cliente(String run,String nombre, String apellido, String patente)
{
try{
conectar();
String SQL = "UPDATE CLIENTE SET Nombre='"+nombre+"', Apellido='"+apellido+"', Patente='"+patente+"' WHERE Rut='"+run+"';";
sentencia.executeUpdate(SQL);
JOptionPane.showMessageDialog(null,"Datos Actualizados");
conexion.close();
sentencia.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error:" +e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
Thank you in advance for any answers you can give us.