我有一个 JTable,其中一个列中包含不同的对象类型。每种类型都有自己的渲染器和编辑器(getDefaultRenderer(Number.class)、我自己的 Date 渲染器/编辑器等)。
但是setValueAt(Object value, int row, int col)
我的表模型中的方法(我覆盖 DefaultTableModel)总是将 String 作为value
. 因此,尽管有不同的编辑器,我无法在不解析字符串的情况下更新行,这不是一个好主意,因为我的表将来应该可以轻松处理其他对象类型。
getValueAt(int row, int col)
不将对象转换为字符串。我检查了一下。这种行为的原因是什么?
编辑:下面的代码。只有布尔单元格编辑器似乎工作正常。
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class Tabela {
public static void main(String[] args){
JFrame fr = new JFrame("tabela");
fr.setSize(600, 400);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// custom renderer and editor for Date
final DateCellEditor dateEditor = new DateCellEditor();
final DateCellRenderer dateRenderer = new DateCellRenderer();
final TableModel model = new TableModel();
JTable table = new JTable(model){
public TableCellEditor getCellEditor(int row, int col){
if (model.getValueAt(row, col) instanceof Boolean)
return getDefaultEditor(Boolean.class);
else if (model.getValueAt(row, col) instanceof Number)
return getDefaultEditor(model.getValueAt(row, col).getClass());
else if (model.getValueAt(row, col) instanceof Date)
return dateEditor;
else return getDefaultEditor(Object.class);
}
public TableCellRenderer getCellRenderer(int row, int col){
if (model.getValueAt(row, col) instanceof Boolean)
return getDefaultRenderer(Boolean.class);
else if (model.getValueAt(row, col) instanceof Number)
return getDefaultRenderer(model.getValueAt(row, col).getClass());
else if (model.getValueAt(row, col) instanceof Date)
return dateRenderer;
else
return getDefaultRenderer(Object.class);
}
};
JScrollPane sc = new JScrollPane(table);
fr.getContentPane().add(sc);
fr.setVisible(true);
}
public static class TableModel extends DefaultTableModel{
// data in first column
private ArrayList<String> names = new ArrayList<String>();
// data in second column - can by any object
private ArrayList<Object> values = new ArrayList<Object>();
public TableModel(){
// insert example data
names.add("string value");
values.add("some string");
names.add("number value");
values.add(new Integer(12345));
names.add("Boolean value");
values.add(new Boolean(false));
names.add("Double value");
values.add(new Double(10.5));
names.add("Date object");
values.add(new Date(System.currentTimeMillis()));
}
public void setValueAt(Object value, int row, int col){
values.set(row, value);
fireTableCellUpdated(row, col);
}
public Object getValueAt(int row, int col){
if (col==0)
return names.get(row) + " ["+values.get(row).getClass().getSimpleName()+"]";
else return values.get(row);
}
public int getRowCount(){
if (values==null) return 0;
else return values.size();
}
public int getColumnCount(){
return 2;
}
public boolean isCellEditable(int row, int col){
return col==1; // only column 2 is editable
}
public Class<?> getColumnClass(int col){
switch(col){
case 0:
return String.class;
default:
return Object.class;
}
}
}
// My own renderer and editor for Date type
protected static class DateCellRenderer extends DefaultTableCellRenderer{
private static DateFormat format = new SimpleDateFormat("dd-MM-y HH:mm:ss");
public static void setDateFormat(String f){
format = new SimpleDateFormat(f);
}
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
if (value instanceof Date){
value = format.format(value);
}
else if(value instanceof Calendar)
{
Calendar dateValue = (Calendar) value;
value = format.format(dateValue.getTime());
}
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (c instanceof JComponent){
JComponent jc = (JComponent) c;
jc.setToolTipText("dd-MM-y HH:mm:ss");
}
return c;
}
}
protected static class DateCellEditor extends DefaultCellEditor{
private static DateFormat format = new SimpleDateFormat("dd-MM-y HH:mm:ss");
private JFormattedTextField textField = new JFormattedTextField(format);
public DateCellEditor(){
super(new JFormattedTextField(format));
}
public void setDateFormat(String f){
format = new SimpleDateFormat(f);
textField = new JFormattedTextField(format);
}
public Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int row, int column)
{ System.out.println("editor: "+value.getClass());
if (value instanceof Date){
value = format.format(value);
}
else if(value instanceof Calendar)
{
Calendar dateValue = (Calendar) value;
value = format.format(dateValue.getTime());
}
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
}