0

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

JLabel, JColorChooser,JCheckBox在每一列中都按此顺序排列,但我不知道如何将其中的任何一个添加到我的 JPanel。此外,我在第三列中添加Booleans了显示为表格内的复选框,当我单击它们时,它们不会编辑(假->真,真->假)。我将所有单元格设置为可编辑。

我有 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) {

    }

}
4

0 回答 0