如何使jtable在java中不可编辑? [英] How to make a jtable not editable in java?

查看:803
本文介绍了如何使jtable在java中不可编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个将数据加载到 JTable 的函数。一切正常,但该表中的所有单元格都是可编辑的。顺便说一句,我使用 defaultTableModel 作为表模型。我在Netbeans IDE中这样做。请帮忙。这是我的代码:

I created a function which loads data into a JTable. Everything's working fine except that all the cells in this table are editable. Btw, I used defaultTableModel for the table model. Im doing this in Netbeans IDE. Please help. Here's my code:

private void updateTable(String searchText){

    if(searchText != null)
        this._sqlCmd = this._sqlCmd + " WHERE "+columnCombo.getSelectedItem()+" LIKE '%"+searchText+"%'";
    jTable1.setSurrendersFocusOnKeystroke(true);
    table = (javax.swing.table.DefaultTableModel) jTable1.getModel();  

    try{
        table.setRowCount(0);
    }catch(Exception e){}

    try {
        ResultSet rs = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(_sqlCmd);
        while (rs.next()){
            Object[] data = new Object[numOfCols];
            for(int i=0; i<data.length; i++){
                data[i] = rs.getObject(i+1);
            }
            table.addRow(data);
        }
        table.fireTableDataChanged();


    } catch (SQLException ex) {
        Logger.getLogger(FindContactGrid.class.getName()).log(Level.SEVERE, null, ex);
    } 
}


推荐答案

 private TableModel model = new DefaultTableModel(data, columnNames)
  {
    public boolean isCellEditable(int row, int column)
    {
      return false;//This causes all cells to be not editable
    }
  };
  private JTable table = new JTable(model);

已编辑。
如果您在 Netbeans IDE设计器中执行此操作,请按照以下步骤操作:

Edited. If you are doing this in Netbeans IDE designer, follow the steps below:



  • 选择放置JTable的表格

  • 从导航窗格中,展开JScrollPane并右键单击JTable并选择自定义代码如下所示:

  • Select the form on which the JTable is placed
  • From the Navigation Pane, expand JScrollPane and right-click on JTable and Select Customize Code as shown below:


  • 在代码自定义程序中,选择第二个下拉菜单并选择自定义属性即可。这使您可以编辑DefaultTableModel代码定义。

  • 现在粘贴它:
    {public boolean isCellEditable(int row,int column){return false; }} 在最后一次关闭blacket 之前;

  • On the code customizer, select the second drop down and choose custom property. This enables you to edit the DefaultTableModel code definition.
  • Now paste this: {public boolean isCellEditable(int row, int column){return false;}} before the last closing blacket );

你的最终设置应如下所示:


  • 按确定保存 - 并完成工作。

这篇关于如何使jtable在java中不可编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆