wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes super(); question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "super(); question" Watch "super(); question" New topic
Author

super(); question

bryan lim
Ranch Hand

Joined: Dec 26, 2008
Posts: 140
hi all,

i wonder what does the super(true); mean in the below code? correct me if i am wrong, super is the parent of this class. but i dont get what it means by super() and why have to do that. thank you

// Sketch.java
// A sketching application with two dials: one for horizontal movement, one
// for vertical movement. The dials are instances of the JogShuttle class.
//
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;

public class Sketch extends JPanel
implements PropertyChangeListener, ActionListener
{
JogShuttle shuttle1;
JogShuttle shuttle2;
JPanel board;
JButton clear;

int lastX, lastY; // Keep track of the last point we drew.

public Sketch() {
super(true);

setLayout(new BorderLayout());
board = new JPanel(true);
board.setPreferredSize(new Dimension(300, 300));
board.setBorder(new LineBorder(Color.black, 5));

clear = new JButton("Clear Drawing Area");
clear.addActionListener(this);

shuttle1 = new JogShuttle(0, 300, 150);
lastX = shuttle1.getValue();
shuttle2 = new JogShuttle(0, 300, 150);
lastY = shuttle2.getValue();

shuttle1.setValuePerRevolution(100);
shuttle2.setValuePerRevolution(100);

shuttle1.addPropertyChangeListener(this);
shuttle2.addPropertyChangeListener(this);

shuttle1.setBorder(new BevelBorder(BevelBorder.RAISED));
shuttle2.setBorder(new BevelBorder(BevelBorder.RAISED));

add(board, BorderLayout.NORTH);
add(shuttle1, BorderLayout.WEST);
add(clear, BorderLayout.CENTER);
add(shuttle2, BorderLayout.EAST);
}

public void propertyChange(PropertyChangeEvent e) {
if (e.getPropertyName() == "value") {
Graphics g = board.getGraphics();
g.setColor(getForeground());
g.drawLine(lastX, lastY,
shuttle1.getValue(), shuttle2.getValue());
lastX = shuttle1.getValue();
lastY = shuttle2.getValue();
}
}

public void actionPerformed(ActionEvent e) {
// The button must have been pressed.
Insets insets = board.getInsets();
Graphics g = board.getGraphics();
g.setColor(board.getBackground());
g.fillRect(insets.left, insets.top,
board.getWidth()-insets.left-insets.right,
board.getHeight()-insets.top-insets.bottom);
}

public static void main(String[] args) {
UIManager.put(JogShuttleUI.UI_CLASS_ID, "BasicJogShuttleUI");
Sketch s = new Sketch();
JFrame frame = new JFrame("Sample Sketch Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(s);
frame.pack();
frame.setVisible(true);
}
}
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
i think it calls the following constructor of JPanel





SCJP 1.5
"A candle looses nothing by lighting another candle"
itechmentors.com
bryan lim
Ranch Hand

Joined: Dec 26, 2008
Posts: 140
thank you and may i know how you know that?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Please use the CODE button for code, not "quote."

The super() call is a standard feature of constructors; it is redundant when it calls an accessible no-arguments constructor in the superclass, but it is essential when the superclass doesn't have a no-arguments constructor. Here it is calling a superclass constructor with an argument.
bryan lim
Ranch Hand

Joined: Dec 26, 2008
Posts: 140
ok thank you for your help.

sorry about the quote part. THANKS!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
You're welcome
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
bryan lim:: thank you and may i know how you know that?


Dude in your code you have inherited JPanel

As far as a constructor is concerned the first statement will be a call to the super call(Parent class constructor)...
if we didn't mention that ,compiler itself will add a no argumented call( like super())...

Here you have added a super(true) call

So it will call the parent class..
i have tried
javap javax.swing.JPanel
...
i got




so super(true) is a call to the parent class contructor with a boolean as argument

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: super(); question
 
Similar Threads
Set mnemonic key
Learning to use Drag and Drop
Border on an rounded JTextField?
Displaying an image in a set location in an applet
JScrollPane Problem