-2

I have a table which is clickable but when I double click, instead of doing what it is told, it goes editing mode. I have tried isCellEditable() method with no success. Maybe I am doing something wrong?

Here is the code:

public AllResultsFromDB(GUI x) {
   final Vector columnNames = new Vector();
   final Vector data = new Vector();


        for (int i = 1; i <= columns; i++) {
            columnNames.addElement(metad.getColumnName(i));
        }


        //  This loop gets the data inside the rows

        while (rset.next()) {
            final Vector row = new Vector(columns);


            for (int i = 1; i <= columns; i++) {
                row.addElement(rset.getObject(i));

            }

            data.addElement(row);
            //data.addElement(b);
        }

        rset.close();
        stmt.close();
        connection.close();


        //  Create table with results

      final JTable  table = new JTable(data, columnNames) {


            public boolean isCellEditable() {
                           return false;
                       }



            public Class getColumnClass(int column) {

                for (int row = 0; row < getRowCount(); row++) {


                    Object obj = getValueAt(row, column);




                    if (obj != null) {

                        return obj.getClass();

                    } 
                } 

                return Object.class;
            }


        };


        JScrollPane scroll = new JScrollPane(table);
        getContentPane().add(scroll);


        JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.SOUTH);


        table.addMouseListener(new MouseListener() {

            public void mousePressed(MouseEvent e) {

                //System.out.println(table.getSelectedRow());

            }

            public void mouseReleased(MouseEvent e) {
                //System.out.println(table.getSelectedRow());

            }

            public void mouseEntered(MouseEvent e) {
                //System.out.println(table.getSelectedRow());

            }

            public void mouseExited(MouseEvent e) {
                //System.out.println(table.getSelectedRow());
            }

            public void mouseClicked(MouseEvent e) {

                if(e.getClickCount()==2){


                System.out.println(table.getSelectedRow());
            }

            }
        });
Sam
  • 7,252
  • 16
  • 46
  • 65
louboulos
  • 17
  • 1
  • 8

2 Answers2

3

The method isCellEditable that you tried to override has a different signature which is:

public boolean isCellEditable(int row, int column) 

How could you specify which specific cell otherwise? Next time adding an @Override annotation should help spotting this.

In any case this is not the correct way to make a JTable non-editable. The correct way is to provide a custom AbstractTableModel which returns false with its isCellEditable method. The JTable shouldn't decide if a cell is editable, it's duty of the model to decide it: indeed the isCellEditable method of JTable just asks to its model if the cell is editable. The JTable shows the content, nothing more, it's the model that decides and contains the data.

Since you seem to use just basic features of a JTable you don't need to roll your own table model, a DefaultTableModel will work for you, and you can overrite its isCellEditable method.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • I don't understand. Is it doable with my code the way it is now? If yes how would you do it? I can't figure it out. – louboulos Oct 01 '13 at 16:06
  • `public boolean isCellEditable(int row, int col) { return false; }` This is the answer. I had just to place this under the table initiation! – louboulos Oct 01 '13 at 17:19
2

You're not that far off.

The actual method isCellEditable takes two parameters, so your method isn't actually overriding anything.

See: http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html#isCellEditable%28int,%20int%29

for the correct method.

BlackBox
  • 2,223
  • 1
  • 21
  • 37
  • That doesn't help me... I know it needs int but I don't know how to do it! Please see my code and tell me what needs to be changed! I am going crazy! – louboulos Oct 01 '13 at 16:20
  • Jack has laid out what is needed. All I can do further is suggest you go through: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html – BlackBox Oct 01 '13 at 17:03