user3599755
user3599755

Reputation: 93

How to make whole JTable uneditable

I want to set setEditable(false) for complete JTable, but I am not aware how to do this. I gone through many sites but everywhere I found this code

DefaultTableModel model = new DefaultTableModel(6,7){
 public boolean isCellEditable(int row, int column)
 {
     return false;
 }
};

Above code seems that specific row and column is non editable but how can I make the complete table non editable.

Here is my code for table where data are coming from MySQL database -

GridBagConstraints gbc = new GridBagConstraints();
jbutton1=new JButton("Close");
setTitle("Customer List");

JPanel panel = new JPanel(new GridBagLayout());
this.getContentPane().add(panel);

String[] columnNames = {"Account No", "Customer Name", "Account Type", "Credit Limit"};

DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);

table = new JTable();
table.setModel(model); 
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

table.setFillsViewportHeight(true);

JScrollPane scroll = new JScrollPane(table);
scroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 

String accno= "";
String name= "";
String type = "";
String limit = "";
try
{
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/banking_prj","root","");
    String sql = "select * from customer ORDER BY account_no";
    PreparedStatement ps = con.prepareStatement(sql);
    ResultSet rs = ps.executeQuery();
    while(rs.next())
    {
        accno = rs.getString(1);
        name = rs.getString(2);
        type = rs.getString(3);
        limit = Double.toString(rs.getDouble(4));
        model.addRow(new Object[]{accno, name, type, limit});
    }
}
catch(Exception ex)
{
    JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",
    JOptionPane.ERROR_MESSAGE);
}

Thank you

Upvotes: 0

Views: 284

Answers (1)

JB Nizet
JB Nizet

Reputation: 691645

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

Above code seems that specific row and column is non editable but how can I make the complete table non editable.

The method returns false, whatever the value of row and column is. So, for all rows and all columns, the corresponding cell is not editable, which is exactly what you want.

Upvotes: 3

Related Questions