8 Replies - 464 Views - Last Post: 05 April 2013 - 10:54 AM Rate Topic: -----

#1 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Subclass not calling superclass constructor

Posted 03 April 2013 - 11:23 PM

As I was setting up some subclasses of DefaultTableRenderer, I realized that I was setting the font the same for each one of them so I decided to create one subclass of DefaultTableRenderer which I called GenericTableRenderer and set the font in there. Then all my other subclasses would extend from GenericTableRenderer. That way if I wanted to change the font on all my tables' columns I can rewrite it once.

But sadly, it is not working. Here is my code.

/**
 * Project: UptownLashes
 * File: GenericTableCellRenderer.java
 * Date: Apr 3, 2013
 * Time: 10:58:55 PM
 */
package uptownLashes.gui.tables.renderers;

import java.awt.Font;

import javax.swing.table.DefaultTableCellRenderer;

/**
 * 
 * @author Brandon Tran, A00753665
 */
@SuppressWarnings("serial")
public class GenericTableCellRenderer extends DefaultTableCellRenderer{

	/**
	 * 
	 */
	public GenericTableCellRenderer() {
		super();
		setFont(new Font("Calibri", Font.PLAIN, 14));	
	}

}



/**
 * Project: UptownLashes
 * File: DateRenderer.java
 * Date: Apr 2, 2013
 * Time: 6:45:51 PM
 */
package uptownLashes.gui.tables.renderers;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.SwingConstants;


/**
 * 
 * @author Brandon Tran, A00753665
 */
@SuppressWarnings("serial")
public class DateRenderer extends GenericTableCellRenderer {
    DateFormat formatter;
    
    public DateRenderer(){
    	super(); 
    	setHorizontalAlignment(SwingConstants.CENTER);
    }

    public void setValue(Object value) {
        if (formatter==null) {
            formatter = new SimpleDateFormat("dd/mm/yyyy");
        }
        try {
			Date date = new SimpleDateFormat("yyyy-mm-dd").parse(value.toString());
	        setText((value == null) ? "" : formatter.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

    }
}



I'm just showing you the DateRenderer but I also one for currency, etc.

I thought that by calling super(); in my DateRenderer that it would call up the setFont() method that is in the GenericTableCellRenderer's constructor.


Thanks.

Toad

This post has been edited by toad87: 03 April 2013 - 11:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Subclass not calling superclass constructor

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Subclass not calling superclass constructor

Posted 04 April 2013 - 02:38 AM

Did you know: If you don't tell the compiler to call super(); on your constructor explicitly, it will anyway?

It calls the default constructors all the way up the inheritance tree until it reaches java.lang.Object - because all classes extend java.lang.Object and java.lang.Object extends nothing.

So, really, putting super(); in there doesn't change anything. If you want to test it out, print the constructor name in each constructor. You'll see what I mean.
Was This Post Helpful? 0
  • +
  • -

#3 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

Re: Subclass not calling superclass constructor

Posted 04 April 2013 - 05:57 AM

I don't think this is a subtyping issue. The super class constructors are called. It's probably the way your trying to apply the font. If you're using JTable, try applying the font to the table (if applicable), or try calling setFont() in setValue().
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Subclass not calling superclass constructor

Posted 04 April 2013 - 12:52 PM

It should...
and if the arguments are the same you don't veven have to call super()

Try

public class DateRenderer extends GenericTableCellRenderer {
    DateFormat formatter;
    
    public DateRenderer(){
      setHorizontalAlignment(SwingConstants.CENTER);
      System.out.println(getFont());
    }
}



Running

public class DateRenderer extends GenericTableCellRenderer {
    
    public DateRenderer(){
      System.out.println(getFont());
    }

    public static void main(String[] args) {
    	new DateRenderer();
    }
}


gives me:

java.awt.Font[family=Calibri,name=Calibri,style=plain,size=14]
Was This Post Helpful? 1
  • +
  • -

#5 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Subclass not calling superclass constructor

Posted 04 April 2013 - 08:05 PM

After some trial and error and looking at something I did for the centering the column headers, this is what I came up:

/**
 * Project: UptownLashes
 * File: GenericTableCellRenderer.java
 * Date: Apr 3, 2013
 * Time: 10:58:55 PM
 */
package uptownLashes.gui.tables.renderers;

import java.awt.Component;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * 
 * @author Brandon Tran, A00753665
 */
@SuppressWarnings("serial")
public class GenericTableCellRenderer extends DefaultTableCellRenderer{

	/**
	 * 
	 */
	public GenericTableCellRenderer() {
		super();
		
	}
	
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
	
		Component c = table.getDefaultRenderer(table.getColumnClass(0)).getTableCellRendererComponent(table,value,isSelected, hasFocus, row, column);
		
		if(JLabel.class.isAssignableFrom(c.getClass())){
			
			((JLabel) c).setFont(new Font("Calibri", Font.BOLD, 14));	
			
		}
		
		return c;
	}

}



I could have just called the JTable's setFont() but this is better for my overall design for my tables.

Thanks for the input guys.

A side question:

What does "setText((value == null) ? "" : formatter.format(date));" mean? I copied that from the JTable tutorial on Sun's website. I'm really just concerned with this part: "(value == null) ? "" : ". I've never seen that before.

This post has been edited by toad87: 04 April 2013 - 08:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Subclass not calling superclass constructor

Posted 05 April 2013 - 03:58 AM

I think it means "setText( is value null? if it is, set text to "", otherwise set it as this formatted 'date' );"

This post has been edited by Flukeshot: 05 April 2013 - 03:59 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Subclass not calling superclass constructor

Posted 05 April 2013 - 07:08 AM

condition ? x : y
means if conditions then x else y
It is more often use as parameter to a function

System.out.println(x == t ? "X and y are equal" : "x and y are not equal");
will print one of the 2 sentences depending if x and y are == or not

so
setText((value == null) ? "" : formatter.format(date));" 



means
if(value == null)
   setText("");
else
   setText(formatter.format(date));


Was This Post Helpful? 0
  • +
  • -

#8 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Subclass not calling superclass constructor

Posted 05 April 2013 - 10:48 AM

I see. So it is a form of if/else. Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Subclass not calling superclass constructor

Posted 05 April 2013 - 10:54 AM

on a single line yes
useful to pass different parameters to a method without having to create temporaty variables
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1