JButton constructor problem
JButton has *** no data fields *** itself and 5 constructors ( may be 8 if you read javadoc):
JButton(){this(null,null);
JButton(Icon icon){this(null,icon);
JButton(String text){this(text,null);
JButton(Action a){this();SetAction(a)};
JButton(String text, Icon icon){setModel(new DefaultButtonModel()); init(text, icon);}
AbstractButton.java has only public static finals, such as
String TEXT_CHANGED_PROPERTY = "text";
String ICON_CHANGED_PROPERTY = "icon";
********** not overwrite-able ***********
and
private String text = ""; /for BeanBox
and NO
private Icon icon;
----
-----
Can anybody explain to me how my system can 'know' how to handle one of the top 3 constructors ?
And is in #4 "this()" short for this(null,null)?
I can understand that there may be an unwritten instruction like
Qconstructor(A,B)
is to be handled as {
this.A = A;
this.B = B;
}
but where can I find that?
Nieuwenhuizen-jk
2011-09-08T13:03
Re: JButton constructor problem
The previous time I was registered as a junior member is about 70 years ago!
Re: JButton constructor problem
Quote:
JButton(){this(null,null);
Why are you adding the {this(null,null); to the above description of the syntax for calling a constructor?
Quote:
And is in #4 "this()" short for this(null,null)?
What are you referring to here? Where have you seen source code like this?
Code :
Qconstructor(first, second)
is to be handled as {
this.A = first;
this.B = second;
}
The names of the parameters are not important.
Re: JButton constructor problem
Quote:
Originally Posted by
Norm
Why are you adding the {this(null,null); to the above description of the syntax for calling a constructor?
What are you referring to here? Where have you seen source code like this?
I believe he's trying to talk about the source of JButton. He's using some wonky syntax, but what he has matches the source of JButton. I think he's asking about how the constructors are chained together.
Re: JButton constructor problem
The gist: this() is a way of calling another constructor. It will simply call the constructor that matches the arguments given. So this is JButton's source:
Basically, the first three constructors (the no-args and the String or Icon constructors) all simply call the two-argument constructor with a dummy value (null) for the missing piece. The constructor that takes an Action calls the first, no-arg constructor (which will then call the two-args constructor) and sets its Action.
The init(text, icon) function is actually in AbstractButton, and it looks like this:
Code java:
protected void init(String text, Icon icon) {
if(text != null) {
setText(text);
}
if(icon != null) {
setIcon(icon);
}
// Set the UI
updateUI();
setAlignmentX(LEFT_ALIGNMENT);
setAlignmentY(CENTER_ALIGNMENT);
}
So, the init() function checks to make sure each argument isn't a dummy value, and calls the appropriate setter for that attribute.
Does that make any sense?
Re: JButton constructor problem
Good observation. It can be hard figuring out what the h... an OP is asking.
Re: JButton constructor problem
Quote:
Originally Posted by
Norm
Good observation. It can be hard figuring out what the h... an OP is asking.
True that. I could also be completely wrong and just talking gibberish!
Re: JButton constructor problem
No, I think you've got it. Your explanation fits very well.
But why is an OP looking at source???
Re: JButton constructor problem
It is all in
jdk1.6.0_22/javax/swingJButton.java lines 61 - 119
I need it since I need to understand what I am doing when making a button within a space of 48 * 40 pixels that has 3 activation fields with
{3 chars text | small image} and this in an array [6][7].
I programmed in C++ under Linux for > 17 years, learn java under {Linux | MAC OS} for programming Android.
If somebody could tell me how to read the /** .... */ comments in jdk1 files, I expect javadoc but do not know how to activate it, I will be grateful.
Re: JButton constructor problem
So did I answer your original question?
Those are indeed javadoc comments, and they are converted into the API: Java Platform SE 6
Re: JButton constructor problem
From jdk1.6.0_22/javax/swing/JButton.java :
line 61:
class JButton extends AbstractButton implements Accessible {
has 5 constructors, no data members and 4 constructors with 2 parameters each. The first parameter is {class String | null}, the second {class Icon | null}
The first 4 constructors have no body.
JButton(){this(null,null);
JButton(Icon icon){this(null,icon);
JButton(String text){this(text,null);
JButton(Action a){this();SetAction(a)};
JButton(String text, Icon icon){setModel(new DefaultButtonModel()); init(text, icon);}
if JButton itself had a data member of class String, named 'text' I had expected a body like
this.text = text;
and similarly with icon. But there are no such fields, there is even no body of the constructor.
The parent, the superclass, is
AbstractButton extends JComponent
but these have no members of those names,
and if they had I expected a statement of the form `super.xxx`.
Therefore the question is simple: How does the compiler handle these bodyless constructors? There is no data member of name 'text', nor of 'Icon' in AbstractButton or Jcomponent, nor in the implementations
Re: JButton constructor problem
Quote:
There is no data member of name 'text', nor of 'Icon' in AbstractButton or Jcomponent
What does the constructor do with the parameters it receives? What internal variables does it assign their values to?
There must be a constructor with code that assigns the passed variables to local variables.
The local variable names do not have to match the parameter names.
Re: JButton constructor problem
Quote:
Originally Posted by
Nieuwenhuizen-jk
The first 4 constructors have no body.
What do you mean by this? Yes they absolutely do.
Quote:
Originally Posted by
Nieuwenhuizen-jk
if JButton itself had a data member of class String, named 'text' I had expected a body like
this.text = text;
and similarly with icon. But there are no such fields, there is even no body of the constructor.
I already explained this to you. They are passed into the init() method of AbstractButton. I even posted the body of that method for you to examine.
Quote:
Originally Posted by
Nieuwenhuizen-jk
The parent, the superclass, is
AbstractButton extends JComponent
but these have no members of those names,
and if they had I expected a statement of the form `super.xxx`.
Like I said, they are passed as parameters to the init() function, which passes them to the setText() and setIcon() methods.
Quote:
Originally Posted by
Nieuwenhuizen-jk
Therefore the question is simple: How does the compiler handle these bodyless constructors?
Again, what do you mean by "bodyless constructors"? The constructors do indeed have bodies. You actually posted the bodies yourself!
Quote:
Originally Posted by
Nieuwenhuizen-jk
There is no data member of name 'text', nor of 'Icon' in AbstractButton or Jcomponent, nor in the implementations
False. From AbstractButton:
Code java:
private String text = ""; (line 144)
private Icon defaultIcon = null; (line 150)
Re: JButton constructor problem
I made an answer yesterday, as far as I know posted it, at 11 sep but do not see it back.
But I looked back and think I understand most of KevinWorkman's reaction of 2011-09-08T13:05:
The compiler does not "understand" how to handle a constructor without data
public JButton() {
this(null, null);
}
and instead go going back where it came from goes to the second, third, fourth
public JButton(Icon icon) {
this(null, icon);
}
public JButton(String text) {
this(text, null);
}
public JButton(Action a) { // skipped, different parameter class
this();
setAction(a);
}
and eventually arrives at
public JButton(String text, Icon icon) {
// Create the model
setModel(new DefaultButtonModel());
// initialize
init(text, icon);
}
finds SetModel in Abstract... line 1718, I don't know where it finds DefaultButtonModel ( not in Abstract..., not in JComponent} although Abstract... uses it in line 1297 and finds init in Abstract...line 2139
But I do not see why this is better than
public JButton(String text, Icon icon) {
if(text != null) { // copied from init(String
setText(text);
}
if(icon != null) {
setIcon(icon);
}
// Set the UI
updateUI();
setAlignmentX(LEFT_ALIGNMENT);
setAlignmentY(CENTER_ALIGNMENT);
}
}
Do I ask to much if I say : "Can you please explain to me why this is not? ( I have an habit in trying to understand why people do better jobs than I do )"
And I am always open for good advices to solve the "TripleButton" problem, sketched before?
Thanks a lot
Nieuwenhuizen
2011-09-12T18:25 CEST
Re: JButton constructor problem
Quote:
Originally Posted by
Nieuwenhuizen-jk
I made an answer yesterday, as far as I know posted it, at 11 sep but do not see it back.
Are you sure it's not on the first page?
Quote:
Originally Posted by
Nieuwenhuizen-jk
But I looked back and think I understand most of KevinWorkman's reaction of 2011-09-08T13:05:
The compiler does not "understand" how to handle a constructor without data
public JButton() {
this(null, null);
}
and instead go going back where it came from goes to the second, third, fourth
public JButton(Icon icon) {
this(null, icon);
}
public JButton(String text) {
this(text, null);
}
public JButton(Action a) { // skipped, different parameter class
this();
setAction(a);
}
and eventually arrives at
public JButton(String text, Icon icon) {
// Create the model
setModel(new DefaultButtonModel());
// initialize
init(text, icon);
}
No. It doesn't go from one to the other to eventually arrive. This line from the no-args constructor:
this(null, null);
Directly calls the two-args constructor.
Quote:
Originally Posted by
Nieuwenhuizen-jk
finds SetModel in Abstract... line 1718, I don't know where it finds DefaultButtonModel ( not in Abstract..., not in JComponent} although Abstract... uses it in line 1297 and finds init in Abstract...line 2139
Huh? What do you mean "where it finds"? What do you mean by any of this, actually?
Quote:
Originally Posted by
Nieuwenhuizen-jk
But I do not see why this is better than
public JButton(String text, Icon icon) {
if(text != null) { // copied from init(String
setText(text);
}
if(icon != null) {
setIcon(icon);
}
// Set the UI
updateUI();
setAlignmentX(LEFT_ALIGNMENT);
setAlignmentY(CENTER_ALIGNMENT);
}
}
It's simply a short cut. People don't love passing in null arguments. That's it. No magic.
Quote:
Originally Posted by
Nieuwenhuizen-jk
Do I ask to much if I say : "Can you please explain to me why this is not? ( I have an habit in trying to understand why people do better jobs than I do )"
What? Why what is not?
Quote:
Originally Posted by
Nieuwenhuizen-jk
And I am always open for good advices to solve the "TripleButton" problem, sketched before?
What TripleButton problem?
You seem to have a pretty basic misunderstanding of how Java works. Before you dive into Swing source code, I highly recommend starting over at the beginner tutorials.