Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Event overhead, does it matter? Date: Wed, 13 Jul 2016 10:45:07 -0400 Organization: Aioe.org NNTP Server Lines: 49 Message-ID: References: <9c586a05-dbbd-4ebc-b61c-a53269421858@googlegroups.com> NNTP-Posting-Host: 7SK6yTFfqwlIPNBLl3l7Qg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:34877 On 7/13/2016 9:51 AM, Eric Douglas wrote: > Is one method more efficient (speed, memory) or is it just a matter of personal preference? > Method 1: > public class MyTree extends JTree implements FocusListener { > scrollPane.addFocusListener(this); > addFocusListener(this); > public void focusGained(final FocusEvent e) { > if (e.getComponent() == this) { > // tree gained focus > } > if (e.getComponent() == scrollPane) { > // scroll pane gained focus > } > } > Method 2: > public class MyTree extends JTree { > scrollPane.addFocusListener(new FocusListener() { > public void focusGained(final FocusEvent e) { > // scroll pane gained focus > } > }); > addFocusListener(new FocusListener() { > public void focusGained(final FocusEvent e) { > // tree gained focus > } > }); You should totally ignore speed and memory considerations. The difference will be white noise. Criteria should be readability and a natural design. I would be inclined to go for method 2, because it is really two focus listeners, using the same and having an if within the methods is a design kludge. Arne