Making JTable cells uneditable

后端 未结 4 581
独厮守ぢ
独厮守ぢ 2020-12-19 23:25

I am trying to make all the cells of a JTable uneditable when double clicked by the user. I have read a lot of forum posts and the general consensus is to create a new table

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 00:01

    Hello Friend am also working on table please try my code

        import javax.swing.table.AbstractTableModel;
    
        public class Table extends AbstractTableModel {
    
        private boolean DEBUG = false;
        private String[] columnNames = {"                Date Time", "                Parameter",
        "   Barometric Pressure (hPa)", "           Temperature (°C)", "             Battery             Voltage    (V)"};
    
    public static Object[][] data = {};
    
    
    public TableControllerViewdataTabTableModel() {
    }
    
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }
    
    @Override
    public int getRowCount() {
        return data.length;
    }
    
    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }
    
    @Override
    public Object getValueAt(int row, int col) {
        return data[row][col];
    }
    
    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    @Override
    public boolean isCellEditable(int row, int col) {
        return false;
    
    }
    
    @Override
    /**
     * The setValueAt.
     */
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    
        if (DEBUG) {
    
            printDebugData();
        }
    }
    
    /**
     * The printDebugData.
     */
    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();
    
        for (int i = 0; i < numRows; i++) {
    
            for (int j = 0; j < numCols; j++) {
            }
    
        }
    
    }
    }
    

提交回复
热议问题