我想爲特定的列設置一個渲染器,但不知何故,這個渲染器並未用於渲染該列。這有什麼解釋嗎?setCellRenderer()不起作用
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);
}
}
的問題似乎realted到的TableModel。有一次,我用它工作perfect.Code我的臺式一個DefaultTableModel:
/**
*
*/
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;
}
}
}
感謝你的幫助
你看看@ http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer –
我非常害怕這個將要求*調試*;爲了調試,我們需要完整的代碼。把你的問題歸結爲一個可運行的小例子,如果這不能告訴你問題本身是什麼,它會給我們其他人一些工作。 – arcy
在你的'LendDataTable'類中,你重寫了['getColumnClass'](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html#getColumnClass(int))方法?這將默認返回Object.class,因此設置Integer的渲染器將不會執行任何操作。 –