aspose file tools*
The moose likes Beginning Java and the fly likes Trouble with my inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with my inner class" Watch "Trouble with my inner class" New topic
Author

Trouble with my inner class

Thomas Knight
Ranch Hand

Joined: Feb 08, 2003
Posts: 33
I am having trouble with an inner class. All I want to do is create a simple class in the JApplet class that will do things such as make the button a certain specified length and width, set it's caption etc). I am able to create the button and have it work, but this code refuses to give the button a caption. Why? Please note that the code I am posting is a very slimmed down version of what I actually have for brevity sake. Thanks.
Here is my code...
[ broke long line in code block -ds ]
[ March 15, 2003: Message edited by: Dirk Schreckmann ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Thomas,
Since you are extending JButton, why do you need a JButton member? It would seem to me that instead of making the call:

you should instead make the call:

The button that you add to the Applet (the NewButon) as far as I can tell never has its caption set.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Thomas Knight
Ranch Hand

Joined: Feb 08, 2003
Posts: 33
Thanks Michael. I tried what you said and it works, but I am not completely sure why
What is the super doing exactly? How does the button constructor know that the Caption variable refers to the label it should have? How would I go about setting other things like color for the button. super(Color.black)??
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Thomas,

What is the super doing exactly?

Your inner class is a JButton. It's important that you fully understand that. When you call super(Caption) it's similar (really the same thing) as calling new JButton(Caption). What was happening in your original code was super() (the default constructor) was being called implicitly before you called Button1 = new JButton(Caption);. That default constructor for a JButton does not set either caption or icon. Any extened class, which is every class except for Object, must call one of its super's constructors. Many times we are oblivious to that fact because we don't actually code it in the constructor, we just let the compiler do it for us.

How does the button constructor know that the Caption variable refers to the label it should have?

Because the JButton(Sting text) constructor, ie super(Caption), creates a JButton and sets the text on the button. That is just the nature of the beast.

How would I go about setting other things like color for the button. super(Color.black)??

You can only call a super constructor once and it must be the first line of code in a construtor. Now, you can have many different constructors and each can make a single super constructor call, but no single object can ever be constucted twice so you can't set any more state thru a different super constructor for any one object. But, once constructed, your fledging object is free to manage its own state and call any public or protected methods (or set any public or protected fields) that it wants to. Setting the color of a swing component is not as simple as calling a setColor method. You have to delve into the UI or look and feel of the component. Try reading the swing tutorial and see if that makes sense to you. You can find it here Creating a GUI with JFC/Swing.
Michael Morris
Thomas Knight
Ranch Hand

Joined: Feb 08, 2003
Posts: 33
Micheal,
Ok, fine. the object is created and the caption is set. I guess what I don't understand is now how do I go about using methonds that buttons have or go about setting its attributes. In the NewButton class What do I put where the question marks are...
ex: ???.setFont(f)
Do I use B1.setFont(f) or NewButton.setFont(f)? Or what. How do I now go about actually changing the attributes of the button created? As always you are a big help. Thanks.
[ March 15, 2003: Message edited by: Thomas Knight ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Thomas,

ex: ???.setFont(f)
Do I use B1.setFont(f) or NewButton.setFont(f)? Or what.

Just setFont(f) or if you like this.setFont(f) or even super.setFont(f). Remember, NewButon is a JButton and as such all public and protected methods and fields defined by JButton and all of its supers can now be called (or set in the case of fields) directly.
Michael Morris
[ March 15, 2003: Message edited by: Michael Morris ]
Thomas Knight
Ranch Hand

Joined: Feb 08, 2003
Posts: 33
Thanks for all your help Michael. I have a better understanding of it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with my inner class
 
Similar Threads
Not able to change Background color of a component
Not to show the ellipse?
how to get paintComponent to respond to a variety of events
How to call variables in one applet using another applet?
What is the best way to create GUI components- in-line, methods, inner classes or separate classes