4

I have overridden the isCellEditable() method of class JTable in my code to make the cells of my JTable non-editable but selectable, but the cells are still editable. How do I solve this problem?

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class A extends JTable{

    JFrame frame = new JFrame();
    Object data[][] = {{"1","Jahanzeb"},{"2","Ahmed"},{"3","Shaikh"}};
    String col[] = {"#","Names"};
    DefaultTableModel tableModel = new DefaultTableModel(data, col);
    JTable table = new JTable(tableModel);
    JScrollPane scroll = new JScrollPane(table);

    public static void main(String arg[]){
        new A();
    }

    public A() {

        table.addMouseListener(new Click());
         table.setModel(tableModel);
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scroll);
        frame.add(table);
        frame.setVisible(true);
    }


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

    class Click extends MouseAdapter{
        public void mouseClicked(MouseEvent e) {
            if(e.getClickCount()==2)
                System.out.println(table.getSelectedRow());
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3808922
  • 61
  • 1
  • 1
  • 3
  • 4
    Its about time you learned how to use the forum properly. You don't just keep posting questions on the same topic (http://stackoverflow.com/a/24726385/131872). Keep the conversation in one thread so everybody knows what has been suggested. Also, you have asked 12 questions and not once have you `accepted` an answer`. – camickr Jul 13 '14 at 22:27

3 Answers3

10

I believe you need to override the isCellEditable() method of the TableModel rather than the JTable, like so:

public class NonEditableModel extends DefaultTableModel {

    NonEditableModel(Object[][] data, String[] columnNames) {
        super(data, columnNames);
    }

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

It is also possible to simply override the method using an anonymous class.

 DefaultTableModel tableModel = new DefaultTableModel(data, col) {

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

This question demonstrates how to perform the override inline, which is handy if you only need to instantiate the TableModel once: How to make a JTable non-editable

h3xStream
  • 6,293
  • 2
  • 47
  • 57
DMunz
  • 253
  • 2
  • 7
2

Your class extends JTable and you override the isCellEditable(...) method.

But then you create a new JTable that you add to the frame and you do NOT override the isCellEditable(..) method of that JTable.

If you want to extend JTable, then don't create a new JTable inside that class.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

You are adding another JTable in your code, you are confusing between the one extending and the other one added to the JFrame!

Add these invocations after fixing the above:

table.setFocusable(false);
table.setRowSelectionAllowed(true);
GingerHead
  • 8,130
  • 15
  • 59
  • 93