The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Hope this helps

Posted by Jay on November 13, 2001 at 1:54 PM

In a nutshell, you need to create a TableModel and set up a JTable with that. You are true, there is no property in JTable to set a cell to be editable. This is because, most of swing components (if not all) are designed according to MVC pattern. In this context, JTable is the view. Obviously, TableModelis the model which determines the table's behaviour. The events control the table. Thats the reason, a table can not set its own(!) cell to be editable. Also, it is recommended that you pass table's data through the model.

The TableModel is the class that contains isCellEditable(int row, int col) method which determines if a cell is editable or not.

That said, there are two ways to create a model

1. Implement TableModel and create a JTable with that.
2. Extend AbstractTableModel

The only use of the abstract class is that it has default behaviour.

if you do not create a TableModel, DefaultTableModel will be instantiated and used within JTable. Within this class, isCellEditable() always returns true and thats the reason your table's cells are editable. Override and make it return false, your goal is accomplished.

Heres a table example with non editable cells. (The data is what I found on my orange soda :)

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.table.AbstractTableModel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TableEx extends JFrame
public TableEx()
JTable table = new JTable(new MyTableModel());
JScrollPane scrollPane = new JScrollPane(table);


setSize(200, 200);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)

public static void main(String[] args)
new TableEx();

class MyTableModel extends AbstractTableModel
Object[][] data = {
{"Calories", "130"},
{"Total Fat", "0%"},
{"Sodium", "1%"},
{"Total Carbs", "12%"},

String[] columns = {"Nutrition Facts", "per serving"};

public int getColumnCount()
return 2;

public int getRowCount()
return 4;

public String getColumnName(int c)
return columns[c];

public Object getValueAt(int rowIndex, int colIndex)
return data[rowIndex][colIndex];

* This is set to false by default in
* AbstractTableModel.
public boolean isCellEditable(int rowIndex, int colIndex)
return false;

  • THANKS!!!! AMARDEEP SHARMA February 09, 2002 at 1:23 PM (0)

Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us