tags:

views:

3447

answers:

5

Button class:

class SubmitButton extends JButton implements ActionListener {
    public SubmitButton(String title){
        super(title);
        ....

Where I declare it:

SubmitButton submit = new SubmitButton("Submit");
submit.setBounds(530+150, 200, 100, 25);

How does super(title) set the String title to the title of the button? How does it do the same as the method .setText() for regular JButtons?

In other words, how did calling super() do the same thing as .setText() how does super() know to change title? is .setText() the only method in JButton Class that takes in string as parameter?

+1  A: 

http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html#JButton(java.lang.String)

(copy paste the entire URL; for some reason the String part is not linked)

It is calling the constructor defined in the JButton class linked above which sets the text of the button to the String passed in. super() is a call to a superclass constructor.

cliff.meyers
I was in the middle of finding a link to the javadocs and my explanation. I am never fast enough.
daub815
+5  A: 

JButton is a central Java Swing class that supports multiple constructors. One of these constructors allows you to set the text for the button label.

Adding super(title) didn't actually make a call - the constructor of the parent would have been called anyway, but it helped select which constructor is invoked.

The setText() operation allows you to change text after button is created but (usually) before it is displayed.

Uri
A good explanation. I usually find these kinda questions, very hard to answer.
Adeel Ansari
Thanks. I used to teach OOP to students so I frequently came across these sort of questions.
Uri
+1  A: 

When the SubmitButton is initialized with the constructor SubmitButton(String title), you call the parent/super class constructor with the title. If you didn't specify the super(title), then the Java compiler would automagically insert the code to call the default constructor of the super class. Then the title would not be set when you create the SubmitButton.

Also, the superclass (JButton) might be using the .setText(string) within its constructor and that is why it performs the same function (need to look at the actual Java source).

http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html?page=2
http://leepoint.net/notes-java/oop/constructors/constructor.html

daub815
Another good explanation. You folks are really good teachers.
Adeel Ansari
+11  A: 
  • SubmitButton extends JButton
  • JButton has a constructor which might look like this (simplified):

    public JButton(String title) {
        setText(title);
    }
    
  • SubmitBUtton has a constructor:

    public SubmitButton(String title) {
        super(title);
    }
    

The SubmitButton constructor is calling the superclass (JButton) constructor, which is in turn calling setText. Now internally, JButton might work differently, but the effect is the same.

The overall point is that super(...) calls the superclass constructor.

Draemon
+2  A: 

In all cases in java, that "call" to super() invokes the parent class's ctor. Like all functions, when you call a ctor, Java pattern matches the name and parameter types. JButton declares a ctor with string argument, so when you call super(title) the effect is that you're invoking the constructor for the immediate superclass (JButton) that takes a single string argument.

Charlie Martin