为什么我无法通过单击更改JTable中的布尔值

时间:2013-05-25 10:34:18

标签: java swing jtable jcheckbox jcolorchooser

我创建了自己的JTable我希望它包含:

每列中按此顺序

JLabelJColorChooserJCheckBox,但我不知道如何将这些内容添加到我的JPanel中。此外,我在第3列中添加了Booleans,它们在表格中显示为复选框,当我点击它们时,它们不会编辑(false - > true,true-> false)。我将我的所有单元格设置为可编辑。

我有JTable:

import javax.swing.JTable;
import javax.swing.table.TableModel;


public class MyJTab extends JTable {
        public MyJTab(TableModel model) {
            super(model);       
        }
}

和模型:

import java.util.ArrayList;

import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;


public class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"ksztalt", "kolor", "stan watku"};
    private ArrayList<RowModel> data;

    public MyTableModel() {
        data = new ArrayList<RowModel>();

    }

    public void addRow(RowModel row){
        data.add(row);
    }
    @Override
    public Class<?> getColumnClass(int columnIndex) {

    /*  return data.get(0).getElement(columnIndex).getClass();*/
        if(columnIndex == 0)
            return JLabel.class;
        if(columnIndex == 1)
            return  JColorChooser.class;
        if(columnIndex == 2)
            return Boolean.class;
        return null;
    }

    @Override
    public int getColumnCount() {
         return columnNames.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
          return data.get(rowIndex).getElement(columnIndex);
    }

    @Override
    public void setValueAt(Object val, int row, int col) {      
          data.get(row).setValue(col, val);
          fireTableCellUpdated(row, col);   
    }

     public boolean isCellEditable(int row, int col) {
             return true;        
     }

}

And the row Model:

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JLabel;


public class RowModel {
Przesuwacz p;
JLabel iconLabel;
JColorChooser colorChooser;
Boolean isMoving;

private final static int ICON_LABEL = 0, COLOR_CHOOSER = 1, CHECK_BOX = 2;
    public RowModel(Przesuwacz p) {
        this.p = p;
        isMoving = new Boolean(true);
        ImageIcon icon =  new ImageIcon(p.getF().getClass().getName().toLowerCase() +".jpg");
        iconLabel = new JLabel();
        iconLabel.setIcon(icon);

    }
    public Object getElement(int columnIndex) {
        if(columnIndex == ICON_LABEL)
            return iconLabel;
        if(columnIndex == COLOR_CHOOSER)
            return colorChooser;
        if(columnIndex == CHECK_BOX)
            return isMoving;
        return null;
    }

    public void setValue(int col, Object val) {

    }

}

0 个答案:

没有答案