如何使jtable在java中不可编辑?

编程入门 行业动态 更新时间:2024-10-22 18:36:14
本文介绍了如何使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中不可编辑?

本文发布于:2023-11-23 01:26:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑   jtable   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!