import javax.swing.*;
import javax.swing.table.*;public class MainFrame extends JFrame {    private Object[][] data = { {"张三", "[email protected]"}, {"李四", "[email protected]"}
    };    private String[] columnNames = {"姓名", "邮箱"};    private TableModel tableModel = new DefaultTableModel(data, columnNames) {        public boolean isCellEditable(int row, int column) {
            boolean fal = false;            TableModel model = tableModel;
            int count = model.getRowCount();            for (int i = 0; i < count; i++) {
                String checked = (String) model.getValueAt(i, 0);
                System.out.println(" 判断是否可以编辑   " + checked);
                if (checked.equals("张三")) {
                    if (row == i) {
                        fal = true;
                    } else {
                        fal = false;
                    }                } else {
                    fal = false;
                }            }
            return fal;
        }    };
    private JTable tblEmp = new JTable(tableModel);
    private JPanel contentPane;    public MainFrame() {
        super("不可编辑的表格");
        setSize(400, 300);
        //
        contentPane = (JPanel) getContentPane();
        JScrollPane pnlMain = new JScrollPane(tblEmp);
        contentPane.add(pnlMain);
    }    public static void main(String[] args) {
        MainFrame f = new MainFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
代码如上:
我想设置在表中姓名为张三的那一列不可以编辑···
我写了上面的代码··实现不了自己想要的那种效果··
请高手指点一下····

解决方案 »

  1.   


    public boolean isCellEditable(int row, int column) {
            TableModel model = tableModel;
            if (model.getValueAt(row, column).equals("张三")) {
                return false;
            }
                return true;
            }这样不就行了?还是你想实现名字为张三的那一行不能编辑?
      

  2.   

    String[][] BookInfo =
    {//实现了双击才可以编辑,单击就不可以了
    {"JAVA","james","one"},
    {"SQL","jacky","two"}
    };
    String [] ColumnName = {"Col1","Col2","Col3"};DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(BookInfo,ColumnName);JTable table = new JTable( dm );
    table.addKeyListener(new CTableKeyAdapter(table));
    TableCellEditor tableCellEditor=table.getDefaultEditor(String.class);
    if (tableCellEditor!=null) {
    if (tableCellEditor instanceof DefaultCellEditor) {
    ((DefaultCellEditor)tableCellEditor).setClickCountToStart(2);
    }
    }
      

  3.   

    乡亲们··我的意思是说··
    不管是选哪一列 ···其余的列 就都不允许编辑了····请问code的实现··
    谢谢·····
      

  4.   

    比如:这么说吧··我根据邮箱的值来进行判断···
    让邮箱为:[email protected]的那一正列都不可以编辑···
      

  5.   

     Object[][] cells = { {Boolean.FALSE, "必选", "", ""}, {Boolean.FALSE,
     "可选",
     "", ""}, {Boolean.FALSE, "必选", "", ""}, {Boolean.FALSE,
     "可选", "", ""}, {Boolean.FALSE, "必选", "", ""},
     {Boolean.FALSE, "必选", "", ""}, {Boolean.FALSE, "必选", "",
     ""}, {Boolean.FALSE, "必选", "", ""}, {Boolean.FALSE, "必选",
     "", ""}, {Boolean.FALSE, "必选", "", ""}, {Boolean.FALSE,
     "必选", "", ""},

     {Boolean.FALSE, "必选", "", ""}, {Boolean.FALSE, "必选", "",
     ""}, {Boolean.FALSE, "可选", "", ""},

     {Boolean.FALSE, "可选", "", ""}, };


    String[] columnNames = { "选择", "重要级", "文件名", "说明", "" ,""};

    // 获得升级MOdule并添加到JTable
    // Object[][] getSetModule=objgetResponse.getModule();
    // String[]
    // moduleName={"ID","name","seleect","depend","release","note"};
    DefaultTableModel model = new DefaultTableModel(cells, columnNames) {



      public boolean isCellEditable(int row, int column)
            {
                TableModel model = jTable1.getModel();

                if (column == 0)
                {
                    String checked = (String) model.getValueAt(row, column + 1);
                    if (checked.equals("必选"))
                    {
                        return false;
                    }
                    return true;
                }
                return false;
            }
    public Class getColumnClass(int columnIndex) {
    return columnIndex == 0 ? Boolean.class : String.class;
    }
    };


    ActionListener listener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    int mode = 0;
    }
    };

    jTable1 = new JTable(model);

    /**
     * 获得用户选择项目
     */
    jTable1.getModel().addTableModelListener(new TableModelListener() {
    public void tableChanged(TableModelEvent e) {
                    
    String getModule = "";
    String getValue = "";

    TableModel model = jTable1.getModel();
    int count = model.getRowCount();
    for (int i = 0; i < count; i++) {
     Boolean checked = (Boolean) model.getValueAt(i, 0);
     if (checked.equals(Boolean.TRUE)) {

     
     getModule = getModule + "" + String.valueOf(i) + ",";
     getValue = getValue + ""
     + (String) model.getValueAt(i, 1) + ",";
     }
    }


    getAllModule = getValue;
    System.out.println(""+getValue);

    }
    });我的表格样式是这样的···
    都是Boolean类型的···
    我想当其中一个发生变化的时候··其余的都不允许编辑了······
    请问··我应该怎么办··
    谢谢····