Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34875 > unrolled thread
| Started by | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| First post | 2016-07-13 06:51 -0700 |
| Last post | 2016-07-13 10:45 -0400 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
Event overhead, does it matter? Eric Douglas <e.d.programmer@gmail.com> - 2016-07-13 06:51 -0700
Re: Event overhead, does it matter? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-13 10:36 -0400
Re: Event overhead, does it matter? Arne Vajhøj <arne@vajhoej.dk> - 2016-07-13 10:45 -0400
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2016-07-13 06:51 -0700 |
| Subject | Event overhead, does it matter? |
| Message-ID | <9c586a05-dbbd-4ebc-b61c-a53269421858@googlegroups.com> |
Is one method more efficient (speed, memory) or is it just a matter of personal preference?
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JScrollPane;
import javax.swing.JTree;
Method 1:
public class MyTree extends JTree implements FocusListener {
private JScrollPane scrollPane;
public MyTree() {
scrollPane = new JScrollPane(this);
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
}
}
public void focusLost(final FocusEvent e) {
if (e.getComponent() == this) {
// tree lost focus
}
if (e.getComponent() == scrollPane) {
// scroll pane lost focus
}
}
}
Method 2:
public class MyTree extends JTree {
private JScrollPane scrollPane;
public MyTree() {
scrollPane = new JScrollPane(this);
scrollPane.addFocusListener(new FocusListener() {
public void focusGained(final FocusEvent e) {
// scroll pane gained focus
}
public void focusLost(final FocusEvent e) {
// scroll pane lost focus
}
});
addFocusListener(new FocusListener() {
public void focusGained(final FocusEvent e) {
// tree gained focus
}
public void focusLost(final FocusEvent e) {
// tree lost focus
}
});
}
}
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-07-13 10:36 -0400 |
| Message-ID | <nm5jm0$5dk$1@dont-email.me> |
| In reply to | #34875 |
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:
> [... one FocusListener shared by two components ...]
>
> Method 2:
> [... two FocusListeners, one for each component ...]
One method is (probably) more efficient. Performing measurements
to discover which is left as an exercise for the reader. (I suspect
that measuring the memory footprints might be comparatively simple,
while measuring the execution speeds might be next to impossible. I
also suspect that neither set of measurements would be worth while.)
As a matter of style, I'd vote for Method 2 on the grounds that
it's more easily modifiable if/when you decide to change the GUI.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2016-07-13 10:45 -0400 |
| Message-ID | <nm5k5j$1r8v$1@gioia.aioe.org> |
| In reply to | #34875 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web