gpt4 book ai didi

java - setCellRenderer() 不起作用

转载 作者:行者123 更新时间:2023-12-01 08:08:18 24 4
gpt4 key购买 nike

我正在尝试为特定列设置渲染器,但不知何故,该渲染器并未用于渲染该列。有什么解释吗?

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

}

问题似乎与 TableModel 有关。一旦我使用 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;


}
}
}

感谢您的帮助

最佳答案

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

下面的方法似乎是问题所在。

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

当您调用 fireTableStructureChange() 时,JTable 将重新创建 TableColumnModel(以及所有 TableColumn),这意味着您的渲染器将不再与 TableColumn 3 关联。

我认为您可以只使用 fireTableDataChanged(),或者如果这不起作用,则使用 fireTableRowsInserted()

如果您愿意,可以查看 DefaultTableModel 的源代码,了解 setDataVector() 方法调用的内容,因为两个模型的概念相同.

关于java - setCellRenderer() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19607636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com