• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI works for first 2 questions but then won't go onto the 3rd question  RSS feed

 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys! I hope you're well! I am having trouble with my GUI. The code is rather long so I have tried to keep it relevant but again I apologize for the long code.
Here is my GameState enum:



And here is my GamePanels class:



Basically you start the program, then you get shown a question with 2 buttons (panel begin), which works fine, then you click btn1 (which has action listener), which activiates pannel middle, working fine, but then if I click the action listener from middle, it will not go onto the end panel.

The odd thing is that if I change code so the first panel action listener goes to the end panel, it works fine so it's almost like it's limited to 2? Does anyone have any idea what is going on? The GUI is basically supposed to replace one JPanel with another which holds different information.

For the full paste bin please look here: pasteBin
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Benjamin,

well, it is not that 'something' is limited to only 2. It is in how you set your actionlisteners
when you create the panels 'begin', 'middle' and 'end'.

Not that there is anything particular wrong, but the fact is that you use only ONE JButton
(called 'btn1') for these panels. And when you create panel 'middle', you reuse this
btn, and you invoke 'addActionListener' on btn1.

But this method does exactly what the name promises: it adds a new ActionListener
to whatever set of ActionListeners may already be present. In this case there is
indeed an existing ActionListener, namely the one that was added in the 'begin'
panel. So, when you click the button in the 'middle' panel, it is still the old
actionlistener that gets called, resulting in, well, the display of the middle panel...

Now, the neatest way to resolve this problem is by giving each panel its own
dedicated buttons.

Another way is: when you create panel 'middle', to first remove any existing
ActionListener. Now, if you look at the API, you see that you need to name
the AL that is to be removed. Since you added an anonymous AL, you do not have
a direct reference to the existing AL. So, one way to get it done nevertheless is the following
(I give the full 'middle()' method here)

(ps: sorry for the ragged comments, but that is what this formatter does!)

And indeed, your coding is a bit, lets say, remarkable. for instance, to get
a reference to your GUI, you create a new instance of Main, just to get
access to its static member 'init'!

But having said that, your code is very nicely laid out and very clear qua structure,
and that made it for me easy to see what is going on!
So I would say: well done!

Greetz,
Piet
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet, thank you so much for time and help.

I did suspect it was an actionListener problem but I always thought that you could dynamically replace an actionListener by assigning a new one, which led me to believe it was a problem with my GUI implementation, if only I tested it with btn2! But your technique to remove the listener isdefinitely a neat trick!

I made the code easier to call by adding it in a method with one parameter, specifying which button listener the method needs to remove:


Which works perfectly. Now I might create another method that automates the whole process. Thanks Piet for the nice comments aswell, I am learning through trial an error so I'm sure I will come back to it one day and have a good laugh ! All the best!
Ben
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good idea, but surely the method should be called removeAllListeners or similar?
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Good idea, but surely the method should be called removeAllListeners or similar?


Technically it's only removing the action listener for the button given in the parameter. However, if I was creating a number of buttons on a panel, with actionListeners then if I wanted to remove all the actionListeners then for sure the more appropriate method should be called RemoveAllListeners();
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is removing all listeners from one button.
 
Piet Souris
Rancher
Posts: 1782
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Benjamin,

indeed a nice addition, this method.

What Campbell means is that this method does indeed remove all ActionListeners from the
button given. If that button only happens to have one AL, then that one AL gets removed.
Has it more AL's , then all these are removed.

But it does not remove ALL listeners, only ActionListeners. You could have a MouseListener,
a KeyboardListener, et cetera, added to the button, These will not get removed.

But this all is not relevant. Let us know when you have your masterpiece ready!

Greetz,
Piet
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take your point; removeAllActionListeners would be a better name. You could also write that as a generic method to remove all instances of a particular kind of Listener from a Component.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!