Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super(); question

 
bryan lim
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think it calls the following constructor of JPanel



 
bryan lim
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you and may i know how you know that?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thank you for your help.

sorry about the quote part. THANKS!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
harilal ithikkat
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic