Enable the jbutton as long as the jtable is not empty. JAVA


It seems to be simple, but this is already taking me 2 days. I simply have a jTable and three buttons. Add, Modify and Delete. The data is added, modified and eliminated perfect. In the JFrame constructor where I have these components, the Modify and Delete buttons leave them inactive (SetEnabled (false)) for logical reason that always in the first instance the jtable will be empty and for that reason I leave them in that state, except the of Add.

But here goes the problem, when adding data to the jtable I want to be enabled the same ones that leave them inactive.

Try the conditional form JTable.GetRowCount, if it was 0 that remain inactive, but, enable them. Probe traversing the jtable with a for by columns and rows (JTable.GetRowCount, Jtable.GetColumnCount) and within it give a condition Jtable.GetValueAt (row, column) == null; but it also does not change its states.

I know that the events of the key (keyReleased) or mouse (mouseClicked or mousePressed) are there, but I can not use them, since at no time I load data within the same file, what I do is load using some jtextfields and with the Add button is saved in the table. With the mouse is not the case either because it does not depend on the Add button (the only case) when I click on it, in any case if the jtable is empty the buttons will not be deactivated.

Now I'm trying the jtable events, which would be the case, but I can not get the right listener to do what I want to happen.

In short, I think I've been quite specific for this and I see that it is not relevant to show the code. In any case if they require it I upload it.

If anyone has any idea of this, I will be eternally grateful with every contribution of help!

asked by Exe Herrera 21.11.2018 в 02:14

1 answer


You could use this validation Remember that a Jtable always has a DefaultTableModel therefore you can always access it to know the data that is displayed in the table, so here I leave this code

boton.setEnabled(tabla.getModel().getRowCount() > 0 ? true : false);
answered by 21.11.2018 в 02:26