求助java JTable!!! 我想在表中改变某一列为JCheckBox或JComboBox,而且想再单独控制每一个JCheckBox或JComboBox,各位大大帮帮忙! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在JTable中修改估计不行吧,JTable中只能添加 文本 内容。要实现你想要的,只能用网格布局,这样每个格子中就可以添加组建了,文本内容只要在格子中添加JTextField组建即可,其他的任何组建都可在你想的地方添加。 你说的单独控制JCheckBox或JComboBox是什么意思?让某一列是JComboBox的编辑器实现起来不难,不清楚你想要的效果是怎么样的 创建你的renderer和Editor,这部分直接复制到你代码里就行了 public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer { public MyComboBoxRenderer(String[] items) { super(items); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); super.setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); } setSelectedItem(value); return this; } } public class MyComboBoxEditor extends DefaultCellEditor { public MyComboBoxEditor(String[] items) { super(new JComboBox(items)); } }一个两列数据的例子,这个根据你的需要改 DefaultTableModel model = new DefaultTableModel(); model.addColumn("A", new Object[]{"item1"}); model.addColumn("B", new Object[]{"item6"}); jTable1.setModel(model); String[][] values = {{"item1", "item2", "item3", "item4"}, {"item6", "item7", "item8", "item9"}}; for (int vColIndex = 0; vColIndex < jTable1.getColumnCount(); vColIndex++) { TableColumn col = jTable1.getColumnModel().getColumn(vColIndex); col.setCellEditor(new MyComboBoxEditor(values[vColIndex])); col.setCellRenderer(new MyComboBoxRenderer(values[vColIndex])); } 换成JCheckBox一个道理,DefaultCellEditor 也支持JCheckBox要稍微改一下MyComboBoxRenderer 详细讲解Java中的封装 继承和多态 关于继承与泛型,谁给解释一下? 求教有关布局问题 如何得到Ftp服务器上的某个文件的属性(文件名,路径,修改日期)? 高分求解 - 网络游戏公司面试题(Java) 请问:我的Applet为什么不能在IIS下运行? Vector中的数据怎样排序(数据是从数据库取出) 很急!请大哥哥帮帮小妹妹! jbuilder6的问题 关于Scanner中nextLine()与next()的问题 新手学习Java关于关键字super有个不解 Swing怎么在树状结构的节点后面添加一个类似复选框的图片?
public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer { public MyComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelectedItem(value);
return this;
}
} public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}一个两列数据的例子,这个根据你的需要改 DefaultTableModel model = new DefaultTableModel();
model.addColumn("A", new Object[]{"item1"});
model.addColumn("B", new Object[]{"item6"});
jTable1.setModel(model);
String[][] values = {{"item1", "item2", "item3", "item4"}, {"item6", "item7", "item8", "item9"}};
for (int vColIndex = 0; vColIndex < jTable1.getColumnCount(); vColIndex++) {
TableColumn col = jTable1.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyComboBoxEditor(values[vColIndex])); col.setCellRenderer(new MyComboBoxRenderer(values[vColIndex])); }
要稍微改一下MyComboBoxRenderer