views:

32

answers:

3

As the title states, I have a JTable and I am unable to edit "arbitrary" columns. I have 4 columns and only the first column is editable. The first column has files and a special editor, the next two columns have Strings, and the last column has Integers. I'm using a custom model, and I am returning true from the isCellEditable method. Of course, I consulted several websites for help first, but I was unable to find anything that helped. I used the java source code to override several JTable methods and insert print statements. In particular, I found that table.editCellAt(row, col) always returns false because the editing component returned from the cell editor is always null. So, I naturally tried to replace the editor using table.setDefaultEditor(String.class, new MyEditor()). Oddly, that DID NOT work. All editors for the String columns were still the GenericEditor that JTable uses by default. I then tried adding the Editors to each column by doing the following:

TableColumnModel model = table.getColumnModel();
for(int i = 1; i < model.getColumnCount(); i++){
    model.getColumn(i).setCellEditor(new MyEditor());
}

Note that i starts at 1 because the first column already has an appropriate editor. I'm out of ideas at this point so I came to the good people at Stack Overflow for some help.

Edit:I am using a DefaultTableModel, I simply overrode isCellEditable to make sure it always returns true (even though DefaultTableModel is supposed to do that be default). I did this to reduce the amount of unhelpful, wasteful debugging responses. Also, the fact that one column is editable but others aren't would seem to indicate the problem is elsewhere.

Edit: It would appear the issue rested with column creation. A professor suggested changing setAutoCreateColumnsFromModel and it seems to have fixed the issue.

A: 

I am using a DefaultTableModel, I simply overrode isCellEditable to make sure it always returns true (even though DefaultTableModel is supposed to do that be default). I did this to reduce the amount of unhelpful, wasteful debugging responses. Also, the fact that one column is editable but others aren't would seem to indicate the problem is elsewhere.

@Brett Geren, @gerenba: I migrated your additional information to the question, so you can delete this. Please use a single identity on the site.
trashgod
+1  A: 

It only take 5 lines of "custom code" to test the usage of a JTable. The rest of the code is a template for any SSCCE you might create in the future.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        DefaultTableModel model = new DefaultTableModel(5, 3);
        JTable table = new JTable( model );
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Basic SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
camickr
A: 

It would appear the issue rested with column creation. A professor suggested changing setAutoCreateColumnsFromModel and it seems to have fixed the issue.

@Brett Geren, @gerenba: I migrated your additional information to the question, so you can delete this. Please use a single identity on the site.
trashgod
Right, and how could we possibly guess that you are using this method since none of the example in the Swing tutorial use this method. That is why you should post a SSCCE with every question.
camickr