• Post Reply Bookmark Topic Watch Topic
  • New Topic

boolean problems with actionPerformed()  RSS feed

 
tor-einar frostad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have a subclass which initializes some JComponents. I've made a method that reurns false if the component is not initialized. For some reason when I click whatever button inside my GUI the first if-statement in actionPreformed gets triggered beforehand. My debugger even tells me that isOpg2GuiMade() returns false, but even then it seems to step into the if statement. I really do not have a clue on what could be wrong here.

checks if subclass components are initialized:



actionPerformed:
 
Paul Clapham
Sheriff
Posts: 22379
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So... that code says

"If the button clicked was the btnNyA button, OR if the result of isOpg2GuiMade(menItNyA, e) is true, then call the dialogs.lagNyAvtale() method."

So if, for example, it was the btnVisA button which was clicked, then to evaluate the result of that OR operation it's necessary to evaluate its right-hand side. Which is that boolean method you're surprised is being called. I strongly believe you wanted

"If the button clicked was the btnNyA button, AND if the result of isOpg2GuiMade(menItNyA, e) is true, then call the dialogs.lagNyAvtale() method."

However I wouldn't fix it that way. I would write a separate ActionListener for each button. The ActionListener for the btnNyA button would look like this:



and the others would be similar.

Edit: However if you do that then it looks to me like the result of the isOpg2GuiMade() method is always going to be false, since for example btnNyA is not equal to menItNyA (or is it?)
 
tor-einar frostad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I wasn't clear enough. I made the method which is called (isOpg2GuiMade) to evaluate if menItNyA is initialized in the current superclasses subclass. I have small modifications to the GUI in that subclass. In order to have the ActionListener classes actionPerformed() method to listen to changes made to the subclasses JComponents, I made field components in the superclass to declare the subclass components, without ever assigning them a value in that class. And so, to avoid a nullPointerException I made that other method. Even if this method seems to return false, when working with the debugger, the if statement returns true. That means if I were to click a button which is neither btnNyA or menItNyA, that statement still returns true. My expections, and what I see from the debugger is "that e.getSource() == btnNyA" returns false because that button was never clicked, isOpgGuiMade(menItNyA, e) should also return false. That is because menItNyA is null, since that variable was never initialized and if it was, e.getSource() == menItNyA would still be false, because I did not click that button. My problem is the FIRST if-statement INSIDE actionPerformed gets called, the statement NOT the method. I am not THAT new to Java. Soon starting my third semester. :)
 
tor-einar frostad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm.. I first thought there might be something about actionPerformed() or booleans that I did not get clearly, but after some analysing I realize this is probably a problem which lies "deeper" in the code. I'm most likely to checkout my master branch and try a different approach. I figured you can not delete a thread once you've made it, or am I wrong?
 
Campbell Ritchie
Sheriff
Posts: 55364
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Paul was clear. He has understood what he needs from your code. That is unfortunately not everything, because some of those names are difficult to read.
Don't test x != null to avoid nulls, but make sure everything is initialised before use. I suggest you need to do several things:-
  • 1: create a setUpGUI method, or you can do all the initialisation in the constructor.
  • 2: Be systematic. Declare and initialise all your display objects, including buttons, in turn. Add listeners to each as Paul has already suggested. Then add the component to the frame, and call setVisible() last.
  • 3: remove implements ActionListener and that actoinPerformed method.


  • I think this thread would fit better in our GUIs forum: moving.
     
    tor-einar frostad
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Ritchie. My project consists of 5 classes and everything was working fine until I started trying to use one actionPreformed() method for both my GUIs, by extending the other, and removing and adding components in the subclass. This is probably not a good aproach anyway, so I guess I could just be fine with overriding actionPreformed in the subclass instead. I'm doing an assignment and got kinda obsessed with not reusing code and such
     
    Paul Clapham
    Sheriff
    Posts: 22379
    42
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    tor-einar frostad wrote:My problem is the FIRST if-statement INSIDE actionPerformed gets called, the statement NOT the method. I am not THAT new to Java. Soon starting my third semester. :)


    Of course the first if-statement inside your actionPerformed method gets called. Why wouldn't it? It's the first line of code in the method. In fact each of the if-statements will be called, unless an unchecked exception is thrown. But obviously you know that already, so you must have meant something different than how I'm interpreting that statement.

    But your real problem is that you wrote a class whose constructor calls a public method, and then you wrote a subclass which overrides that public method. This is guaranteed to make a big mess (I've encountered it myself and it's a huge pain). But apparently you're trying to work around it with this idea of testing things which shouldn't be null to see if they are null. I tried that too and it was just a bigger mess. It may be ugly but I recommend putting all of your initialization code in the constructors, so that it gets done in the right order. Alternatively don't put any initialization code in the constructors, but put it all in the setupGUI() methods and call that method from outside the class, after the constructor has finished.

    Edit: Unless you're wondering why the actionPerformed method is being called at all?
     
    Campbell Ritchie
    Sheriff
    Posts: 55364
    157
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    tor-einar frostad wrote:Thanks Ritchie.
    My pleasure to help
    . . . I could just be fine with overriding actionPreformed in the subclass instead. . . . obsessed with not reusing code and such
    Avoid sunclassing display classes (=GUI Components) as much as possible. Create your own classes for all Listeners and don't put actionPerformed methods in any of the other classes. I have strong opinions about Listeners, mentioned here and in the two older links. You can create public classes which implement ActionListener, or private classes, or even anonymous classes:-Fortunately now that λs are available in Java8 you can simplify that greatly:-That requires a start() method somewhere.

    You should not worry about reusing code. I know that for assessed work you are supposed to write all new code, but in your current application, the more you can reuse the better. That is one of the principles of programming: the more you reuse the better.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!