0

I am trying to set a Renderer for a specific Column, but somehow this renderer is not being used for rendering that column. Is there any explanation for that?

tabledata = new LendDataTable();
table.setModel(tabledata);
TableColumn xx = table.getColumnModel().getColumn(3);
xx.setCellRenderer(new BookBackRenderer());//here it doesn't (there are 7 rows in total)
table.setDefaultRenderer(Integer.class, new BookBackRenderer());// here it works
add(table, BorderLayout.CENTER);



public  class BookBackRenderer extends DefaultTableCellRenderer {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public BookBackRenderer() {
    // TODO Auto-generated constructor stub

}

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {


    System.out.println(column);
    return super.getTableCellRendererComponent(table, value.toString() + "xy", isSelected, hasFocus, row, column);
}

}

the problem seems to be realted to the TableModel. Once I use a DefaultTableModel it works perfect.Code for my table model:

/**
 * 
 */
package client.gui;

import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EventObject;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.text.DateFormatter;

import org.json.JSONArray;

import lbvs.Leiheintrag;

/**
 * @author John
 *
 */
public class LendDataTable extends AbstractTableModel  implements
TableModel{
    private List<Leiheintrag> lendlist;

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * 
     */
    public LendDataTable() {
        // TODO Auto-generated constructor stub
        lendlist = new LinkedList<Leiheintrag> ();
    }
    public void setData (List<Leiheintrag> list)
    {
        lendlist = list;
        this.fireTableStructureChanged();
        this.fireTableDataChanged();



    }

    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getRowCount()
     */
    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return lendlist.size();
    }

    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getColumnCount()
     */
    @Override
    public int getColumnCount() {
        // TODO Auto-generated method stub
        return 7;
    }

    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getValueAt(int, int)
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // TODO Auto-generated method stub
        Leiheintrag eintr = lendlist.get(rowIndex);
        switch (columnIndex){
        case 0:
            return eintr.getBuch();
        case 1:
            return eintr.getLeihdatum();
        case 2:
            return eintr.getAbgabe_bis();
        case 3:
            return ((Long)eintr.getRueck_datum());
        case 4:
            return eintr.getBezahlt_am();
        case 5:
            return eintr.getKosten();
        case 6 :
            return eintr.getLast_edit_user();
         default:
            return null;


        }
    }
    public Class<?> getColumnClass(int columnIndex){
        switch (columnIndex){
        case 0:
            return Integer.class;
        case 1:
            return Long.class;
        case 2:
            return Long.class;
        case 3:
            return Long.class;
        case 4:
            return Long.class;
        case 5:
            return Float.class;
        case 6 :
            return Integer.class;
         default:
            return null;


        }
    }
}

thank you for your help

mKorbel
  • 109,525
  • 20
  • 134
  • 319
joz
  • 652
  • 2
  • 8
  • 19
  • Did you take a look @ http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer – NG. Oct 26 '13 at 14:19
  • 1
    I am very much afraid this is going to require *debugging*; in order to debug, we need complete code. Boil your problem down to a small runnable example, and if that doesn't show you what the problem is itself, it will give others of us something to work with. – arcy Oct 26 '13 at 14:19
  • In your `LendDataTable` class have you overridden the [`getColumnClass`](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html#getColumnClass(int)) method? This returns `Object.class` by default so setting a renderer for `Integer` will do nothing. – Boris the Spider Oct 26 '13 at 14:41
  • yeah I did.. but the problem is that the Renderer schould be used for a specific column and should not be defined by the class of the value (because that is the same in different columns) ... – joz Oct 26 '13 at 14:45

2 Answers2

5

the problem seems to be realted to the TableModel. Once I use a DefaultTableModel it works perfect

The method below would appear to be the problem.

public void setData (List<Leiheintrag> list)
{
    lendlist = list;
    this.fireTableStructureChanged();
    this.fireTableDataChanged();
}

When you invoke fireTableStructureChange() the JTable will recreate the TableColumnModel (and all the TableColumns) which means your renderer will no longer be associated with TableColumn 3.

I think you can just use the fireTableDataChanged(), or if that doesn't work then use fireTableRowsInserted().

If you want you can take a look at the source code of the DefaultTableModel to see what the setDataVector() method invokes because the concept is the same for both models.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

what @camickr is saying, is apparently true. But i think there is one important things to mention.

The JAVA DOC of fireTableStructureChanged clearly states that:

with JTable.setModel(model) call or upon receiving the event fired by fireTableStructureChanged function call, if autoCreateColumnsFromModel is set(true) JTable will discards any table columns that it had and reallocates default columns in the order they appear in the model. Instead of removing fireTableStructureChanged() call, i think setting autoCreateColumnsFromModel flag to false is sufficient. To set or unset this flag use:

JTable.setAutoCreateColumnsFromModel(boolean)
Sage
  • 15,290
  • 3
  • 33
  • 38
  • Yes, setting autoCreateColumnFromModel(false) will work (and it is important to know about this method), but the real problem is because the setData() method was not implemented correctly. It is better to fix the problem than mask the symptom. – camickr Oct 26 '13 at 20:09
  • @camickr, I agree with you. :) – Sage Oct 26 '13 at 20:45