Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35745 > unrolled thread
| Started by | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| First post | 2016-10-11 06:51 -0700 |
| Last post | 2016-10-11 22:08 +0200 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
Is there a better way to reference this? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-11 06:51 -0700
Re: Is there a better way to reference this? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-11 10:25 -0400
Re: Is there a better way to reference this? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-11 07:37 -0700
Re: Is there a better way to reference this? Sebastian <nowhere@nirvana.org> - 2016-10-11 22:08 +0200
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2016-10-11 06:51 -0700 |
| Subject | Is there a better way to reference this? |
| Message-ID | <7fd3908d-6705-4034-99ee-08e4e3a4a4c6@googlegroups.com> |
How to grab the instance from the inside without assigning?
Sample:
HashSet<JTable> storeTable = new HashSet();
JTable myTable = new JTable() {
{
final JTable ref = this;
addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
storeTable.add(ref);
}
@Override
public void focusLost(FocusEvent e) {
}
});
}
};
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-10-11 10:25 -0400 |
| Message-ID | <ntisp7$u03$1@dont-email.me> |
| In reply to | #35745 |
On 10/11/2016 9:51 AM, Eric Douglas wrote:
> How to grab the instance from the inside without assigning?
> Sample:
>
> HashSet<JTable> storeTable = new HashSet();
> JTable myTable = new JTable() {
> {
> final JTable ref = this;
> addFocusListener(new FocusListener() {
>
> @Override
> public void focusGained(FocusEvent e) {
> storeTable.add(ref);
> }
>
> @Override
> public void focusLost(FocusEvent e) {
> }
>
> });
>
> }
> };
I think you're out of luck as things stand. If you were to use a
named class as your JTable subclass you could get rid of `ref' and use
`MyTableClass.this' inside the FocusListener, but as long as the JTable
extension remains anonymous I don't think there's any way to qualify
the `this'.
(Disclaimer: I'm no language lawyer, just a language paralegal.)
--
esosman@comcast-dot-net.invalid
The Tooth Fairy has gone high-tech, now pays in Bitecoin.
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2016-10-11 07:37 -0700 |
| Message-ID | <6f6d13d3-c131-4502-ab97-ae8043d33d26@googlegroups.com> |
| In reply to | #35746 |
On Tuesday, October 11, 2016 at 10:26:10 AM UTC-4, Eric Sosman wrote: > I think you're out of luck as things stand. If you were to use a > named class as your JTable subclass you could get rid of `ref' and use > `MyTableClass.this' inside the FocusListener, but as long as the JTable > extension remains anonymous I don't think there's any way to qualify > the `this'. Ah OK, so there's that at least. This simple extension of the JTable was the shortest I could come up with for sample code. So if we have MyCustomTable extends JTable then create a listener inside a method of MyCustomTable we can reference the instance of the MyCustomTable the listener is being added to with the MyCustomTable.this. I have seen that syntax before, haven't realized a practical use for it.
[toc] | [prev] | [next] | [standalone]
| From | Sebastian <nowhere@nirvana.org> |
|---|---|
| Date | 2016-10-11 22:08 +0200 |
| Message-ID | <ntjgrl$oqr$1@news.albasani.net> |
| In reply to | #35747 |
Am 11.10.2016 um 16:37 schrieb Eric Douglas:
> On Tuesday, October 11, 2016 at 10:26:10 AM UTC-4, Eric Sosman wrote:
>> I think you're out of luck as things stand. If you were to use a
>> named class as your JTable subclass you could get rid of `ref' and use
>> `MyTableClass.this' inside the FocusListener, but as long as the JTable
>> extension remains anonymous I don't think there's any way to qualify
>> the `this'.
>
> Ah OK, so there's that at least.
> This simple extension of the JTable was the shortest I could come up with for sample code. So if we have MyCustomTable extends JTable then create a listener inside a method of MyCustomTable we can reference the instance of the MyCustomTable the listener is being added to with the MyCustomTable.this. I have seen that syntax before, haven't realized a practical use for it.
>
Here's another approach: use lambdas. A lambda does not introduce a new
scope for variable references, in particular 'this' means the same
inside a lambda as in the surrounding context. So if your FocusListener
were a lambda, it could reference the surrounding JTable - anonymous or
not - by 'this'.
Now FocusListener, containing as it does two abstract methods, is not a
functional interface. But you might define an abstract utility class
'FocusListeners' with the following reusable factory methods:
public static FocusListener forFocusGained(Consumer<FocusEvent> c) {
return new FocusAdapter() {
public void focusGained(FocusEvent e) {
c.accept(e);
}
};
}
public static FocusListener forFocusLost(Consumer<FocusEvent> c) {
return new FocusAdapter() {
public void focusLost(FocusEvent e) {
c.accept(e);
}
};
}
There is one one static factory method for each FocusListener method.
Each accepts a lambda expression as a parameter whose signature
corresponds to that of the relevant FocusListener method and returns a
listener instance that invokes the lambda expression.
With that, your application code becomes very concise:
HashSet<JTable> storeTable = new HashSet<>();
JTable myTable = new JTable() {
{
addFocusListener(forFocusGained(e -> storeTable.add(this)));
}
Of course, you can make lambda-enabled MouseListeners, KeyListeners etc.
in the same way.
This approach is due to Heinz Kabutz.
-- Sebastian
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web