column resized and edit cell at not working
Ok, I'm uncertain if this is a bug or not, but it sure feels like one.
I've noticed that if a column resize is done then a following editCellAt is not working. I've written a small class showing this problem. (Explained below.)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
publicclass TestJTableResize
{
/**
* @param args
*/
publicstaticvoid main(String[] args)
{
// setup a table
DefaultTableModel dm =new DefaultTableModel();
String[] columnNames ={"This is going to be a really long column header","Column B","Column C","Column D","Column E","Column F","Column G","Column H","Column I","Column J"};
Integer[][] data =new Integer[8][10];
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 10; ++col)
{
data[row][col] =new Integer((row * 100) + col);
}
}
dm.setDataVector(data, columnNames);
final JTable lJTable =new JTable(dm);
// grow
JButton lJButton1 =new JButton("grow");
lJButton1.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent arg0)
{
lJTable.getColumnModel().getColumn(1).setPreferredWidth( lJTable.getColumnModel().getColumn(1).getPreferredWidth() + 10);
lJTable.editCellAt(1, 1);
System.out.println(lJTable.isEditing() +"/" + lJTable.getEditingRow() +"/" + lJTable.getEditingColumn());
}
});
// shrink
JButton lJButton2 =new JButton("shrink");
lJButton2.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent arg0)
{
lJTable.getColumnModel().getColumn(1).setPreferredWidth( lJTable.getColumnModel().getColumn(1).getPreferredWidth() - 10);
lJTable.editCellAt(1, 1);
System.out.println(lJTable.isEditing() +"/" + lJTable.getEditingRow() +"/" + lJTable.getEditingColumn());
}
});
// edit
JButton lJButton3 =new JButton("edit");
lJButton3.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent arg0)
{
lJTable.editCellAt(1, 1);
System.out.println(lJTable.isEditing() +"/" + lJTable.getEditingRow() +"/" + lJTable.getEditingColumn());
}
});
// show it
JFrame lJFrame =new JFrame();
lJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lJFrame.setLayout(new BorderLayout());
lJFrame.add(lJTable, BorderLayout.CENTER);
JPanel lJPanel =new JPanel();
lJPanel.setLayout(new BoxLayout(lJPanel, BoxLayout.LINE_AXIS));
lJPanel.add(lJButton1);
lJPanel.add(lJButton2);
lJPanel.add(lJButton3);
lJFrame.add(lJPanel, BorderLayout.SOUTH);
lJFrame.setSize(600, 300);
lJFrame.setVisible(true);
}
}
The class shows a table with some dummy data, and three buttons. The first two buttons will make column 1 grow or shrink and then try to edit cell (1,1). The third button wil only try to edit the cell. For all JVM's I've tested (1.4, 1.5 and 1.6) the first two buttons do not start an edit, even though the JTable is absolutely convinced it is editing (see console output). Even when tracking the keyboard focus (code not included), it shows that the focus is in the cell editor.

