Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: JButton constructor problem

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Location
    Eindhoven, NL
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Location
    Eindhoven, NL
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JButton constructor problem

    The previous time I was registered as a junior member is about 70 years ago!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JButton constructor problem

    JButton(){this(null,null);
    Why are you adding the {this(null,null); to the above description of the syntax for calling a constructor?

    And is in #4 "this()" short for this(null,null)?
    What are you referring to here? Where have you seen source code like this?

    Qconstructor(first, second) 
     
    is to be handled as { 
    this.A = first;
    this.B = second;
    }
    The names of the parameters are not important.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JButton constructor problem

    Quote Originally Posted by Norm View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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:

        public JButton() {
            this(null, null);
        }
     
        public JButton(Icon icon) {
            this(null, icon);
        }
     
        public JButton(String text) {
            this(text, null);
        }
     
        public JButton(Action a) {
            this();
    	setAction(a);
        }
     
        public JButton(String text, Icon icon) {
            // Create the model
            setModel(new DefaultButtonModel());
     
            // initialize
            init(text, icon);
        }

    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:


    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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JButton constructor problem

    Good observation. It can be hard figuring out what the h... an OP is asking.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JButton constructor problem

    Quote Originally Posted by Norm View Post
    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!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JButton constructor problem

    No, I think you've got it. Your explanation fits very well.

    But why is an OP looking at source???

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Location
    Eindhoven, NL
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Location
    Eindhoven, NL
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JButton constructor problem

    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.

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JButton constructor problem

    Quote Originally Posted by Nieuwenhuizen-jk View Post
    The first 4 constructors have no body.
    What do you mean by this? Yes they absolutely do.

    Quote Originally Posted by Nieuwenhuizen-jk View Post
    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 View Post
    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 View Post
    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 View Post
    There is no data member of name 'text', nor of 'Icon' in AbstractButton or Jcomponent, nor in the implementations
    False. From AbstractButton:
        private String     text                    = ""; (line 144)
        private Icon       defaultIcon             = null; (line 150)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Junior Member
    Join Date
    Sep 2011
    Location
    Eindhoven, NL
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JButton constructor problem

    Quote Originally Posted by Nieuwenhuizen-jk View Post
    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 View Post
    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 View Post
    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 View Post
    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 View Post
    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 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. constructor X is undefined
    By ober0330 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 24th, 2011, 01:51 PM
  2. how to solve the constructor problem? " return new Accounts
    By defyzer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 10th, 2011, 11:16 AM
  3. Problem with java swing JButton in Netbeans IDE
    By vrp in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 12th, 2011, 11:38 PM
  4. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 25th, 2010, 12:15 AM
  5. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM