Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Requesting help with question being asked. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Requesting help with question being asked." Watch "Requesting help with question being asked." New topic
Author

Requesting help with question being asked.

kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54
I'm not sure how to answer this question and would appreciate some advice:

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?

Why is the decision statement mentioned in the previous question necessary?

Here is the method the question is referring to:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If it's not a JButton, then it would be some other Swing component which supports ActionListeners.

And if that were the case, then casting it to a JButton in line 4 of the posted code would throw a ClassCastException. That would be a bad thing; doing nothing would be better, and that's what line 1 does for you.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Where did you get that code, kenny? If it's provided as a sample of bad code that you have to improve, fine. But as an example of Swing and Java coding, it's atrocious.

For making an identity comparison using the == operator, casting is redundant. And since casting isn't needed, neither is the instanceof test.

And apart from all that, it suggests that a class having some other purpose of its own implements ActionListener, which is usually bad style.


luck, db
There are no new questions, but there may be new answers.
 
wood burning stoves
 
subject: Requesting help with question being asked.
 
Similar Threads
Problem with "abstract class
how to flash JButton background
Jtable and database results
Help with actionlisteners on a button.
Lotto generation