0

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).

Image 1

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045

1 Answers1

3

In general, because database access is inherently asynchronous, you should perform data manipulation in the background, typically using SwingWorker. A complete example is shown here. In your context, disable the button prior to starting the worker thread.

button.setEnabled(false);
worker.execute();

Enable the button in your implementation of the worker's done() method, as shown here.

@Override
protected void done() {
    button.setEnabled(true);
}       

Addendum: Looking closer at your particular use case, you can disable the desired button in your implementation of the prepareEditor() method of JTable, as suggested here.

@Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
    Component c = super.prepareEditor(editor, row, column);
    button.setEnabled(false);
    return c;
}

Because JTable is a CellEditorListener, you can enable the button in your implementation of the editingStopped() method of JTable, as discussed here.

@override
public void editingStopped(ChangeEvent ce) {
    super.editingStopped(ce);
    button.setEnabled(true);
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for your answer!! I was looking for information about how to implement "prepareEditor" & "editingStopped" but i just dont understand how. I need to create a Class? Where, in the main Jframe? another class? Never used "@override" before, sorry to bother you, we are students and your solution is something that we have not learned or seeing yet. Im gona try to ask my teacher personally, but i dont think he knows the answer. Thank you very much on advance – Delirio Rimbombante Nov 02 '16 at 13:12
  • Our jTables are contained by a jFrame. Maybe i should go to "customize code" on each jTable properties? – Delirio Rimbombante Nov 02 '16 at 13:28
  • I mean, all examples ive seen, don't look even a bit like our project. We just have a JFrame then all public voids like "show", "insert" are there, then we have 3 "mantainers" for each "client","vehicle" & "register". We have nothing else. We must create a new Package? new Interface? – Delirio Rimbombante Nov 02 '16 at 14:28
  • I would limit the scope of the GUI builder, for [example](http://stackoverflow.com/a/2561540/230513), until you understand more, and study the examples cited to learn how `JTable` works with JDBC. – trashgod Nov 02 '16 at 18:28