0

I have written this code for getting the index of the row which is double clicked by the user, the problem is that I want to make the cells non editable as well, for that I have extended JTable and overridden its isCellEditableMethod(int,int); I have also tried all answers already present but I am not getting my results. Please help I just want the cells of my JTable non editable but selectable.

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);
        table.setFocusable(false);
        table.setRowSelectionAllowed(true);
        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());
        }
    }


}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3808922
  • 61
  • 1
  • 1
  • 3
  • I have applied all the previous answers but they are not satisfactory. Please tell me the solution. – user3808922 Jul 13 '14 at 22:58
  • You created your last question only 2 hours ago. You may need to wait longer, or in the worst case scenario you may not ever get a satisfactory solution. You are not guaranteed to get an answer on stack overflow. – Christian Wilkie Jul 13 '14 at 23:01
  • `I have applied all the previous answers but they are not satisfactory.` - no you did not apply the suggestion given. Twice (in two different postings) the suggestion was to extend the DefaultTableModel. You extended JTable (and did NOT do it correctly). – camickr Jul 13 '14 at 23:12
  • I have also tried to extend DefaultTableModel but did not work. If I have applied it incorrectly then please tell the correct way. – user3808922 Jul 13 '14 at 23:21
  • I am not a mind reader, I don't know how you applied the changes to the DefaultTableModel. Read the answers in you other posting. This posting is dead because it has been closed. – camickr Jul 13 '14 at 23:28

0 Answers0