jtable中插入jtextfield问题
重写了tablemodel之后想往jtable中插入jcombox或者jtextfield均无效果,代码如下
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
Vector<Object> destFieldRow = null;
while (it.hasNext()){
fields = (TableFields)it.next();
destFieldsCombobox.addItem(fields.getFieldName());
if (fields.isAllowEmpty() == false){
destFieldRow = new Vector<Object>();
destFieldRow.add(fields.getFieldName());
destFieldRow.add(fields.getFieldType());
destFieldRow.add(fields.getFieldLength());
destFieldRow.add(fields.isAllowEmpty());
destFieldRow.add("");
data.add(destFieldRow);
}
}
DestSetTable destSetModel = new DestSetTable(data);
JComboBox c = new JComboBox();
c.addItem("测试");
destSetTable.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(c));
destSetTable.setModel(destSetModel);
--------------------编程问答-------------------- http://blog.csdn.net/mengxiangyue/article/details/6966701
class DestSetTable extends AbstractTableModel{
@SuppressWarnings("rawtypes")
Vector<Vector> data = new Vector<Vector>();
String[] n = {"字段名", "类型", "长度", "允许为空", "默认值"};
@SuppressWarnings({ "rawtypes", "unchecked" })
public DestSetTable(Vector data){
this.data = data;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return n.length;
}
public String getColumnName(int col){
return n[col];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data.get(rowIndex).get(columnIndex);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public Class getColumnClass(int c){
return getValueAt(0, c).getClass();
}
@SuppressWarnings("unchecked")
public void setValueAt(Object value, int row, int col) {
data.get(row).set(col, value);
fireTableCellUpdated(row, col);
}
public boolean isCellEditable(int rowIndex, int columnIndex)
{
if (columnIndex == 4){
return true;
}
else{
return false;
}
}
}
看看这个 --------------------编程问答-------------------- 就是这么写的啊,问题是显示出来没效果 --------------------编程问答--------------------
destSetTable.setModel(destSetModel)不setmodel就能出来,用了这段jtextfield跟jtable都没法显示 --------------------编程问答-------------------- 自己顶下,希望大牛帮忙解答一下,本人菜鸟个 --------------------编程问答--------------------
import javax.swing.JFrame;
import java.awt.Button;
import javax.swing.JComboBox;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class ItemsListeners extends JFrame{
String province[]={"广东","江苏","湖南"};
String city[][]={{"广州","东莞","深圳"},
{"苏州","无锡","南京"},
{"长沙","湘潭","株洲"}};
JComboBox pro;
JComboBox city1;
public ItemsListeners(){
setLayout(new FlowLayout());
pro=new JComboBox(province);
city1=new JComboBox(city[0]);
add(pro);
add(city1);
pro.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent itemevent) {
int s=pro.getSelectedIndex();
city1.removeAllItems();
for(int i=0;i<city[s].length;i++)
{
city1.addItem(city[s][i]);
}
}
});
}
public void showme(){
setSize(200,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new ItemsListeners().showme();
}
}
//界面有点简陋,反正有效果就行!在选择省份后,后边的城市也会相应改变
--------------------编程问答-------------------- 研究一下这个类 TableCellEditor --------------------编程问答-------------------- setTableModel的时候好像覆盖了设置的celleditor,是不是要在重写的tablemodel里写方法呢,本人菜鸟,还请大牛指导下咯 --------------------编程问答-------------------- 兄弟没看懂我的问题咯,不是jcombobox的连动咯,是在jtable中嵌入jcombobox的问题,我复写了tablemodel,显示按我上面的那些代码jcombobox显示不出来咯 --------------------编程问答-------------------- 1楼那个可以。已验证。
补充:Java , Java SE