怎样在JTable里设定单元格的cellrender 大家好,JTable好像只能设置一列的cellrender我想设置指定单元格的cellrender,而不是整列,请问应该怎么做呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 请问用prepareRenderer可以实现吗? jtable.setCellSelectionEnable(true); 呵呵,我以前和你差不多,不过我是设置editor,当时找了很多资料,以下是我设置指定单元格的CellEditor的方法,希望对你有帮助。EachRowEditor.javaimport java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.*;import javax.swing.event.*;public class EachRowEditor implements TableCellEditor { protected Hashtable editors; //哈希表 protected TableCellEditor editor, defaultEditor; //编辑器 JTable table; //这个变量似乎没用到 public EachRowEditor(JTable table) { this.table = table; editors = new Hashtable(); defaultEditor = new DefaultCellEditor(new JTextField()); //用JTextField构造编辑器 } public void setEditorAt(int row, TableCellEditor editor) { //关键 editors.put(new Integer(row),editor); //指定编辑器加入哈希表 } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { return editor.getTableCellEditorComponent(table, value, isSelected, row, column); } public Object getCellEditorValue() { return editor.getCellEditorValue(); } public boolean stopCellEditing() { return editor.stopCellEditing(); } public void cancelCellEditing() { editor.cancelCellEditing(); } public boolean isCellEditable(EventObject anEvent) { selectEditor((MouseEvent)anEvent); return editor.isCellEditable(anEvent); } public void addCellEditorListener(CellEditorListener l) { editor.addCellEditorListener(l); } public void removeCellEditorListener(CellEditorListener l) { editor.removeCellEditorListener(l); } public boolean shouldSelectCell(EventObject anEvent) { selectEditor((MouseEvent)anEvent); return editor.shouldSelectCell(anEvent); } protected void selectEditor(MouseEvent e) { //这个没看明白 int row; if (e == null) { row = table.getSelectionModel().getAnchorSelectionIndex(); } else { row = table.rowAtPoint(e.getPoint()); } editor = (TableCellEditor)editors.get(new Integer(row)); if (editor == null) { editor = defaultEditor; } }}设置用以下方法:EachRowEditor rowEditor = new EachRowEditor(JTable); //JTable那个参数似乎没用,null应该也可以rowEditor.setEditorAt(行号, new DefaultCellEditor(jComboBox1)); //用jComboBox1构造编辑器,并指定行 jTable1.getColumnModel().getColumn(列号).setCellEditor(rowEditor); //设置编辑器 上面的"行号","列号"为int型,两个int便指定了你想要的单元格 请教关于linux下java类DateFormat的使用! 如何用ASP.NET(VB)把图片存入ACCESS里去(急!!~~!!~~!!~~!!~~!!~~!!~~!!) 内部类如何实例化 初学者问个简单的问题 關于ResultSet 关于url 哪位能讲一下assert(断言) 求《华容道》的编程方法(思路)和源程序!! 尽量避免使用DriverManager方法。遗憾的是,许多JDBC教科书中还在经常使用这种方法。 Java2核心技术卷II 有段代码不知道哪里有错误请赐教 如何在unix下用java操作ms word??
EachRowEditor.javaimport java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;public class EachRowEditor implements TableCellEditor {
protected Hashtable editors; //哈希表
protected TableCellEditor editor, defaultEditor; //编辑器
JTable table; //这个变量似乎没用到 public EachRowEditor(JTable table) {
this.table = table;
editors = new Hashtable();
defaultEditor = new DefaultCellEditor(new JTextField()); //用JTextField构造编辑器
} public void setEditorAt(int row, TableCellEditor editor) { //关键
editors.put(new Integer(row),editor); //指定编辑器加入哈希表
} public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) { return editor.getTableCellEditorComponent(table,
value, isSelected, row, column);
} public Object getCellEditorValue() {
return editor.getCellEditorValue();
}
public boolean stopCellEditing() {
return editor.stopCellEditing();
}
public void cancelCellEditing() {
editor.cancelCellEditing();
}
public boolean isCellEditable(EventObject anEvent) {
selectEditor((MouseEvent)anEvent);
return editor.isCellEditable(anEvent);
}
public void addCellEditorListener(CellEditorListener l) {
editor.addCellEditorListener(l);
}
public void removeCellEditorListener(CellEditorListener l) {
editor.removeCellEditorListener(l);
}
public boolean shouldSelectCell(EventObject anEvent) {
selectEditor((MouseEvent)anEvent);
return editor.shouldSelectCell(anEvent);
} protected void selectEditor(MouseEvent e) { //这个没看明白
int row;
if (e == null) {
row = table.getSelectionModel().getAnchorSelectionIndex();
} else {
row = table.rowAtPoint(e.getPoint());
}
editor = (TableCellEditor)editors.get(new Integer(row));
if (editor == null) {
editor = defaultEditor;
}
}
}
设置用以下方法:
EachRowEditor rowEditor = new EachRowEditor(JTable); //JTable那个参数似乎没用,null应该也可以
rowEditor.setEditorAt(行号, new DefaultCellEditor(jComboBox1)); //用jComboBox1构造编辑器,并指定行
jTable1.getColumnModel().getColumn(列号).setCellEditor(rowEditor); //设置编辑器