2 qq 22896917 qq_22896917 于 2014.12.15 19:45 提问

关于JTable渲染器Renderer的问题,求高手解答!!!!
 public class MyButtonRender implements TableCellRenderer
    {
        private JPanel panel;
        private JButton add,reduce;
        private JTextField numbertf;
        public MyButtonRender()
        {
            this.initAdd();
            this.initReduce();
            this.initNumbertf();
            this.initPanel();
            // 添加按钮
            this.panel.add(this.add);
            this.panel.add(this.numbertf);
            this.panel.add(this.reduce);
        }
        private void initAdd()
        {
            this.add = new JButton("+");
            // 设置按钮的大小及位置。
            this.add.setBounds(0, 0, 41, 20);
        } 
        private void initReduce(){
            this.reduce = new JButton("-");
            // 设置按钮的大小及位置。
            this.reduce.setBounds(82, 0, 41, 20);
        }     
        private void initNumbertf(){
            this.numbertf=new JTextField();
            this.numbertf.setBounds(42,0,30,20);
        }
        private void initPanel()
        {
            this.panel = new JPanel();
            // panel使用绝对定位,这样button就不会充满整个单元格。
            this.panel.setLayout(null);
        }
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
                int column)
        {
            return this.panel;
        }
    }

这是我渲染器的代码,就是在一个单元格内添加两个按钮和一个文本框

 public  class AllTableModel extends AbstractTableModel{
    private static final long serialVersionUID = 1L;
    protected Vector content=null;
    private String[] title_name = { "编号", "名称", "型号","数量"};
    public void addRow(String ID, String name,String model,JPanel number) {
        Vector v = new Vector(title_name.length);
        v.add(0,ID);
        v.add(1,name);
        v.add(2, model);
        v.add(3,number);
        content.add(v);
   }         
    //需要子类自行提供addRow方法    
     public AllTableModel(){
           content = new Vector();
     }   
     public AllTableModel(int count){
           content = new Vector(count);
     }       
     public void removeRow(int row) {
           content.remove(row);
     }       
     public void removeRows(int row, int count) {
             for (int i = 0; i < count; i++) {
                 if (content.size() > row) {
                     content.remove(row);
                 }
             }
     }   
     public int getColumnCount() {
         String[] inherit=getTitlename();
         return inherit.length;
     } 
     public int getRowCount() {
         return content.size();
     }   
     public Object getValueAt(int row, int col) {
         return  ((Vector) content.get(row)).get(col);
     }   
     public String getColumnName(int col) {
         String[] inherit=getTitlename();
         return inherit[col];
     } 
     public void setValueAt(Object value, int row, int col) {
             ((Vector) content.get(row)).remove(col);
             ((Vector) content.get(row)).add(col, value);
             this.fireTableCellUpdated(row, col);
     }   
     public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            return false;
        }
        return true;
    }
    //需要在子类中定义自己的getTitlename方法
    public String[] getTitlename(){
         return title_name;
    }
}

这是我的TableModel的代码

我想问为什么我用DefaultTableModel,然后用两个数组初始化,就可以显示出来渲染器的效果,但是要是用自己写的TableModel,然后调用addRow方法添加,表格中就什么也不显示,这是为什么?

Csdn user default icon
上传中...
上传图片
插入图片