jQuery in Action, 2nd edition*
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "super(); question" Watch "super(); question" New topic

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() {

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");

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



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.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.fillRect(insets.left, insets.top,

public static void main(String[] args) {
UIManager.put(JogShuttleUI.UI_CLASS_ID, "BasicJogShuttleUI");
Sketch s = new Sketch();
JFrame frame = new JFrame("Sample Sketch Application");
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"
bryan lim
Ranch Hand

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

Joined: Oct 13, 2005
Posts: 37945
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

Joined: Oct 13, 2005
Posts: 37945
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

I agree. Here's the link: http://aspose.com/file-tools
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