Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could someone tell me if I answered this question correctly?

 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm being asked this question:

In the actionPerformed(ActionEvent event) method in the class InputPanel, the first line of code is a decision statement determining if the event.getSource is a JButton. If the source is NOT a JButton, what else would it be?


Here is the class the question is referring to:




I believe it will be setSales. Is this correct? Thank you for any help given!
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I got as far as line 7, the answer was no. Even without reading the rest of it.
The source could in theory be anything. I am pretty sure that text components (including text fields) can fire action events if you push the enter key when they have focus, so that is one possibility. The instanceof operator is used to test whether the following cast is safe. So you now have three non‑object‑oriented features for the price of one:-
  • Making a GUI component a listener, unless it is listening for events originating in itself.
  • Casting
  • Use of instanceof.
  • You only have one source for the events, so add the listener to that source. Create a class which handles that event. Not the panel class.
    Lots more in that code:-
  • You have almost certainly got too many fields in that class. A lot of those fields should be local variables.
  • Why are you returning an array of text fields from your method?
  • Why are you using int[]s rather than creating a Salesman class and having Salespeople arrays?
  • Why are you using "north", etc., for the Border Layout rather than the constants defined in the Border Layout class?


  • I shall move this discussion to our GUI forum because there is so much GUI‑related stuff to the question.
     
    Paul Clapham
    Sheriff
    Pie
    Posts: 20764
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This code:



    is the only place where the InputPanel object is added as an ActionListener to any component. So the question is just as bad as the code is.

    Why are you being asked this question?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic