I have an array of JButtons which form a keypad interface. After six numbers are entered I want to disable the keypad so that no further numbers can be entered by the user.
I have written the code and the buttons do disable until the mouse hovers above any of them, then the buttons seem to re-enable themselves and run actionEvents added to them.
The full code is available here.
Possible things that I think are wrong.
- There is some sort of MouseListener which is ignoring when I set
button.setEnabled(false); - I haven’t separated attributes from the
buildGUI();correctly, I only did this anyway so that the inner class could access them. - Possibly something to do with the
gridLayoutas disabling the buttons seems to work for myservicesJPanel buttons.
The problem lies in how you instantiated your Frame (
CashMachine), not (directly) with its implementation.You are calling
buildGUItwice, one in the object’s constructor, and then in theDriverclass that instantiates the object. As a result, you are creating (and laying out) two sets of buttons.When the buttons of the first set were eventually disabled, your mousing activity was revealing the second set of buttons. And a flaw in your ActionListener implementation can cause
inputCountto take on values greater than 6, so buttons in the second set were not eventually disabled like those from the first set.buildGUIshould be private; it should be called in the CashMachine constructor, and not by yourDriverclass.Conversely, in my opinion,
CashMachine.setVisibleshould be called by theDriverclass, and not by theCashMachineconstructor.