Disable user edit in JTable

心不动则不痛 提交于 2019-11-26 22:50:56

问题


When a JTable component is created, cell editing is enabled by default. How can I prevent the user from editing the content of a JTable?


回答1:


A JTable uses an AbstractTableModel object. This is the thing you pass into the constructor of the JTable. You can write your own AbstractTableModel as follows

public class MyTableModel extends AbstractTableModel {

      public boolean isCellEditable(int row, int column){  
          return false;  
      }

}

and then initialize your JTable as

JTable myTable = new JTable(new MyTableModel());



回答2:


You can create a JTable using following code:

    JTable jTable = new JTable() {
        private static final long serialVersionUID = 1L;

        public boolean isCellEditable(int row, int column) {                
                return false;               
        };
    };

Basically what we are doing here is overriding isCellEditable and always returning false from it. This will make a non editabe JTabel.




回答3:


myTable.setDefaultEditor(Object.class, null);



回答4:


Hi I'm working a lot on java so I'm gonna give you my way: There are two possibilities the first under netbeans. Go to customize code and make it like this:

JTArticleJPAddArrticle = new javax.swing.JTable();

JTArticleJPAddArrticle.setBackground(new java.awt.Color(204, 204, 255));

JTArticleJPAddArrticle.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
    "Reference","Libellé","Marque","Prix d'achat","Prix de vente","Quantité","Total","Etat"
}
){
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return false;
}
});



jScrollPane8.setViewportView(JTArticleJPAddArrticle);

My other way is to do it is to make an instance of the table model. This is the second way:

model=new DefaultTableModel(head, 0){

    @Override
    public boolean isCellEditable(int i, int i1) {
        return false; //To change body of generated methods, choose Tools | Templates.
    }

   };
jtable.setmodel(model);

Enjoy this is working well for me. All I want to do is help you guys out because I was helped out a lot earlier.




回答5:


Have you tryed simply:

JTable table = new JTable();
table.setEnabled(false);

About JComponent.setEnabled(boolean) it sayes:

Sets whether or not this component is enabled. A component that is enabled may respond to user input, while a component that is not enabled cannot respond to user input. Some components may alter their visual representation when they are disabled in order to provide feedback to the user that they cannot take input.

When it comes to JTable it doesnt seem to give any visual feedback at all. With the perk of still being able to click on the column headers. And in my implementation the application could still change the contents of the cells.




回答6:


Well on netbeans you can right click on the table and click on table contents, then you go to the column tab and uncheck the "Editable" checkbox. Greetings!!




回答7:


I know I am late but hope someone get use of this. You can simple add mouse listener like this:

jtable.addMouseListener( new MouseAdapter () {
    @Override
    public void mouseClicked ( MouseEvent e ) {
        columnIndex = replacedAssets.getSelectedColumn ();
        System.out.println ( "Double click on jtable" );
        if ( columnIndex == 1 || columnIndex == 2 ) {
            JOptionPane.showMessageDialog ( parent , "Editing this Field may cause error in the data." , "Error Edit Not Permitted For This Field" , JOptionPane.ERROR_MESSAGE );
        }
    }
});

this code prevent editing the columns of indexes 1 and 2 you can remove the if condition to make this work for all columns.




回答8:


        tm = new javax.swing.table.DefaultTableModel()
                 {
                      public Class<?> getColumnClass(int column)
                      {
                        switch(column)
                        {
                        case 0:
                          return String.class;
                        case 1:
                          return String.class;
                        case 2:
                          return String.class;
                        case 3:
                          return String.class;
                        case 4:
                          return String.class;
                        case 5:
                              return String.class;
                            case 6:
                              return String.class;
                            case 7:
                              return String.class;
                            case 8:
                              return String.class;
                            case 9:
                                  return String.class;
                                case 10:
                                  return String.class;
                                case 11:
                                    return Boolean.class;

                          default:
                            return String.class;
                        }
                      }

                      @Override
                      public boolean isCellEditable(int row, int column) {
                         /* Set the 11th column as editable and rest non-
                              editable */
                          if(column==11){
                              return true;
                          }else{
 //all other columns to false
                         return false;
                          }
                      }
                    };
    table = new javax.swing.JTable(tm);

In this method "isCellEditable" we can enable and disable user edit for particular column. In this case enable column=11 and disable rest of the column



来源:https://stackoverflow.com/questions/9919230/disable-user-edit-in-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!