For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2007 > Trying to create a working internal confirm JOptionPane









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Trying to create a working internal confirm JOptionPane
phillip.s.powell@gmail.com

2007-02-20, 7:07 pm

code:
public class SimpleBrowser extends BrowserBean { // BrowserBean extends JFrame class LayoutManager { /** * Handle {@link #urlLabelText} to change color if {@link java.net.URL} displayed is not {@link #homeURL} */ private void handleURLLabelText() { l = new JLabel(urlLabelText); if (getURL() != null && getHomeURL() != null && ! getURL().equals(getHomeURL())) { l.setForeground(Color.RED); JOptionPane.showConfirmDialog(this.getClass().getSuperclass(), "Do you wish to set \"" + getURLPath() + "\" as your default homepage?", "Set as new homepage", JOptionPane.OK_CANCEL_OPTION); } } } }


I am trying to correctly use JOptionPane.showConfirmDialog() in order
to set up an confirm option pane embedded internally into the instance
of the class SimpleBrowser. However, upon doing so I get the
following compiler error:

quote:

Cannot find symbol
symbol: method showConfirmDialog(java.lang.Class< capture of ? super
capture of ? extends
com.ppowell.tools.ObjectTools.SimpleBrowser.LayoutManager
>,java.lang.String,java.lang.String,int)

location: class javax.swing.JOptionPane



Not sure exactly what I need to put into showConfirmDialog() to make
it work, based on the architecture above, what do you recommend?

Thanks
Phil

Andrew Thompson

2007-02-20, 7:08 pm

On Feb 21, 9:22 am, "phillip.s.pow...@gmail.com"
<phillip.s.pow...@gmail.com> wrote:
> [code]

....
> class LayoutManager {

....
> JOptionPane.showConfirmDialog(this.getClass().getSuperclass(),
> "Do you wish to set \"" + getURLPath() + "\"


What does 'this.getClass().getSuperclass()'
return? Do you know how to find out?

Andrew T.

phillip.s.powell@gmail.com

2007-02-20, 7:08 pm

On Feb 20, 5:39 pm, "Andrew Thompson" <andrewtho...@gmail.com> wrote:
> On Feb 21, 9:22 am, "phillip.s.pow...@gmail.com"
>
> <phillip.s.pow...@gmail.com> wrote:
> ...
> ...
>
> What does 'this.getClass().getSuperclass()'
> return? Do you know how to find out?


Absolutely a guess but I would think it would return the instance of
SimpleBrowser, the containing class that extends BrowserBean which
extends JFrame (which extends Component)

>
> Andrew T.



Daniel Dyer

2007-02-20, 7:08 pm

On Wed, 21 Feb 2007 00:01:37 -0000, phillip.s.powell@gmail.com
<phillip.s.powell@gmail.com> wrote:

> On Feb 20, 5:39 pm, "Andrew Thompson" <andrewtho...@gmail.com> wrote:
>
> Absolutely a guess but I would think it would return the instance of
> SimpleBrowser, the containing class that extends BrowserBean which
> extends JFrame (which extends Component)
>


You don't need to guess, it's in the API documentation
(http://java.sun.com/javase/6/docs/api/). Take a look at the return types
of Object.getClass() and the getSuperclass() method. Then have a look at
the type of the first parameter of showConfirmDialog.

Dan.


--
Daniel Dyer
https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
phillip.s.powell@gmail.com

2007-02-21, 8:06 am

On Feb 20, 7:26 pm, "Daniel Dyer" <"You don't need it"> wrote:
> On Wed, 21 Feb 2007 00:01:37 -0000, phillip.s.pow...@gmail.com
>
>
>
> <phillip.s.pow...@gmail.com> wrote:
>
>
>
>
> You don't need to guess, it's in the API documentation
> (http://java.sun.com/javase/6/docs/api/). Take a look at the return types
> of Object.getClass() and the getSuperclass() method. Then have a look at
> the type of the first parameter of showConfirmDialog.


Object.getClass().getSuperclass() returns type Class. I need type
Component there. This makes absolutely no sense to me because this is
LayoutManager extending SimpleBrowser which ultimately extends
JFrame. Sorry, not following whatsoever.

>
> Dan.
>
> --
> Daniel Dyerhttps://watchmaker.dev.java.net- Evolutionary Algorithm Framework for Java



Andrew Thompson

2007-02-21, 7:16 pm

On Feb 22, 12:46 am, "phillip.s.pow...@gmail.com"
<phillip.s.pow...@gmail.com> wrote:
> On Feb 20, 7:26 pm, "Daniel Dyer" <"You don't need it"> wrote:

...
>
> Object.getClass().getSuperclass() returns type Class. I need type
> Component there. This makes absolutely no sense to me because this is
> LayoutManager extending SimpleBrowser


No. From what I understand of your original code,
LayoutManager is an *inner* class. Since it makes
no explicit 'extends' declaration, it will, by
default, extend Object.

>...which ultimately extends
> JFrame. Sorry, not following whatsoever.


I am not sure how to change your original code
to do what you want. It is not remotely close
to being compilable, aven if SimpleBrowser is
changed for JFrame.

If you post an SSCCE (and please strip the
JDoc comments, they are largely a waste of
space in an SSCCE) I might be able to assist
further.

Andrew T.

Daniel Dyer

2007-02-21, 7:16 pm

On Wed, 21 Feb 2007 13:46:00 -0000, phillip.s.powell@gmail.com
<phillip.s.powell@gmail.com> wrote:

> On Feb 20, 7:26 pm, "Daniel Dyer" <"You don't need it"> wrote:
>
> Object.getClass().getSuperclass() returns type Class. I need type
> Component there. This makes absolutely no sense to me because this is
> LayoutManager extending SimpleBrowser which ultimately extends
> JFrame. Sorry, not following whatsoever.


There are two misunderstandings here:

1). The method you are using returns an instance of the special class
java.lang.Class. That Class instance may represent the java.awt.Component
type or one of its sub-types (it doesn't in this case because of the
second point below), but that is not the same as being an instance of
java.awt.Component. You can use a java.lang.Class instance to create new
instances of the class it represents, but that won't help in your
situation as you want to get a reference to an existing instance.

2). LayoutManager does not extend SimpleBrowser, it is an inner class of
SimpleBrowser. So your approach wouldn't work even if the first point was
not valid.

You can obtain a reference to the instance of the outer class from within
an inner class by qualifying the 'this' keyword as follows:

OuterClass.this

Where 'OuterClass' is the name of the containing class.

Dan.

--
Daniel Dyer
http://www.uncommons.org
phillip.s.powell@gmail.com

2007-02-21, 7:16 pm

On Feb 21, 9:12 am, "Daniel Dyer" <"You don't need it"> wrote:
> On Wed, 21 Feb 2007 13:46:00 -0000, phillip.s.pow...@gmail.com
>
>
>
> <phillip.s.pow...@gmail.com> wrote:
>
>
>
>
>
>
>
> There are two misunderstandings here:
>
> 1). The method you are using returns an instance of the special class
> java.lang.Class. That Class instance may represent the java.awt.Component
> type or one of its sub-types (it doesn't in this case because of the
> second point below), but that is not the same as being an instance of
> java.awt.Component. You can use a java.lang.Class instance to create new
> instances of the class it represents, but that won't help in your
> situation as you want to get a reference to an existing instance.
>
> 2). LayoutManager does not extend SimpleBrowser, it is an inner class of
> SimpleBrowser. So your approach wouldn't work even if the first point was
> not valid.
>
> You can obtain a reference to the instance of the outer class from within
> an inner class by qualifying the 'this' keyword as follows:
>
> OuterClass.this
>
> Where 'OuterClass' is the name of the containing class.
>
> Dan.


I used that and it works, thanx.. however, even after reading the
tutorials on Sun I am still .

Why do I use OuterClass.this? Isn't that calling the outerclass
statically? I simply do not understand, even in spite of the
tutorials I don't get it.


>
> --
> Daniel Dyerhttp://www.uncommons.org



Lew

2007-02-21, 7:16 pm

phillip.s.powell@gmail.com wrote:
> Why do I use OuterClass.this? Isn't that calling the outerclass
> statically? I simply do not understand, even in spite of the
> tutorials I don't get it.


In an inner class, "this" by itself points to the inner class instance. The
inner class, because it is an inner class, belongs to an object of the outer
class. That means there is an outer class "this", and the language
specification says that to obtain a reference to the outer class instance, the
syntax is
Foo.this
where Foo is the outer class.

This is a matter of language syntax. Just because it looks like a static
reference doesn't make it one. "this" is a keyword, not a variable name.

Similarly, when you see "this()" as the first line of a constructor, it is not
a reference to an object but an invocation of another constructor. The syntax
specifies rules for what "this" and other symbols mean, and they differ for
different situations.

It is a mistake to think that just because "this." means an object reference
that it somehow should mean that in the "this()" expression. The syntax says
that they mean different things.

- Lew
phillip.s.powell@gmail.com

2007-02-21, 7:16 pm

On Feb 21, 9:43 am, Lew <l...@nospam.lewscanon.com> wrote:
> phillip.s.pow...@gmail.com wrote:
>
> In an inner class, "this" by itself points to the inner class instance. The
> inner class, because it is an inner class, belongs to an object of the outer
> class. That means there is an outer class "this", and the language
> specification says that to obtain a reference to the outer class instance, the
> syntax is
> Foo.this
> where Foo is the outer class.
>
> This is a matter of language syntax. Just because it looks like a static
> reference doesn't make it one. "this" is a keyword, not a variable name.
>
> Similarly, when you see "this()" as the first line of a constructor, it is not
> a reference to an object but an invocation of another constructor. The syntax
> specifies rules for what "this" and other symbols mean, and they differ for
> different situations.
>
> It is a mistake to think that just because "this." means an object reference
> that it somehow should mean that in the "this()" expression. The syntax says
> that they mean different things.
>
> - Lew


You completely lost me.

Lew

2007-02-21, 7:16 pm

phillip.s.pow...@gmail.com wrote:

Lew wrote:[color=darkred]

phillip.s.powell@gmail.com wrote:[color=darkred]
> You completely lost me.


I am saying that "this" is a keyword with a few different rules, and that you
cannot confuse one rule with the other.

You are talking about a core Java concept, the keyword "this", the
understanding of which is fundamental to using Java. You should know the rules
for the keyword.

There are three usages.

- a reference to the instance itself, as in "this.value = value;"
- as the first line of a constructor, the invocation of another constructor in
the class, as in
public Foo()
{
this( DEFAULT );
}

- as a reference to the instance of an outer class that owns this inner class,
the topic of your question. This last one is more advanced than the other two.

Your confusion came from seeing case #3 as similar to a static variable
reference. It is not a static variable reference, but a completely different
part of the Java language. Your mistake was thinking that one part of the Java
language should follow rules from another part.

- Lew
phillip.s.powell@gmail.com

2007-02-21, 7:16 pm

On Feb 21, 10:41 am, Lew <l...@nospam.lewscanon.com> wrote:
> phillip.s.pow...@gmail.com wrote:
> Lew wrote:
>
> phillip.s.pow...@gmail.com wrote:
>
> I am saying that "this" is a keyword with a few different rules, and that you
> cannot confuse one rule with the other.
>
> You are talking about a core Java concept, the keyword "this", the
> understanding of which is fundamental to using Java. You should know the rules
> for the keyword.
>
> There are three usages.
>
> - a reference to the instance itself, as in "this.value = value;"
> - as the first line of a constructor, the invocation of another constructor in
> the class, as in
> public Foo()
> {
> this( DEFAULT );
> }


That one lost me completely, sorry.

>
> - as a reference to the instance of an outer class that owns this inner class,
> the topic of your question. This last one is more advanced than the other two.


> Your confusion came from seeing case #3 as similar to a static variable
> reference. It is not a static variable reference, but a completely different
> part of the Java language. Your mistake was thinking that one part of the Java
> language should follow rules from another part.
>


Not sure why you feel that is the means of my confusion, but
nonetheless, though I have no choice but to blindly follow it, I don't
personally like the fact that I can have:

if (SimpleBrowser.isValidURL("http://www.sun.com")) {
SimpleBrowser.this.setURL(new URL("http://www.sun.com"));
}

because the visual syntax is confusing into thinking this.setURL() is
a static reference of SimpleBrowser when it's not. That was my point.

> - Lew



Lew

2007-02-21, 7:16 pm

phillip.s.powell@gmail.com wrote:
> Not sure why you feel that is the means of my confusion, but
> nonetheless, though I have no choice but to blindly follow it, I don't
> personally like the fact that I can have:
>
> if (SimpleBrowser.isValidURL("http://www.sun.com")) {
> SimpleBrowser.this.setURL(new URL("http://www.sun.com"));
> }
>
> because the visual syntax is confusing into thinking this.setURL() is
> a static reference of SimpleBrowser when it's not. That was my point.


It is only confusing because you have not learned to recognize the difference
between when "this" is to the right of the dot vs. when a variable or method
name is to the right of the dot.

Once you get used to it, and get over liking or disliking it, you will see how
it makes sense.

Try this thought experiment - from an inner class, what syntax would you
invent to refer to the outer object's "this" if you were designing Java?

Then accept what Sun did.

Lew said:

phillip.s.powell@gmail.com wrote:[color=darkred]
> That one lost me completely, sorry.


Study the syntax and effect of the constructor calls "this(...)" and
"super(...)", which must appear as the first line of a constructor if they are
used.

When something like an explanation of constructor call "this()" "loses" you,
how about you Google for it, or study the Sun tutorials, or the Java Language
Specification?

- Lew

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com