Re: JTable cell selection is broken
Here is the test case:
package test.table;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
class TableEdit implements ActionListener {
JTable table;
TableEdit()
{
JFrame frame = new JFrame("Editing Test");
frame.setBounds(10,10,750,550);
MyTableModel model = new MyTableModel();
table = new MyTable(model);
Container c = frame.getContentPane();
JScrollPane scrollpane = new JScrollPane(table);
c.add(scrollpane, BorderLayout.CENTER);
JButton loadAnotherTable = new JButton("Load");
loadAnotherTable.addActionListener(this);
c.add(loadAnotherTable, BorderLayout.SOUTH);
frame.setVisible(true);
}
public static void main(String [] args)
{
TableEdit appln = new TableEdit();
}
public void actionPerformed(ActionEvent e) {
table.clearSelection();
}
}
package test.table;
import java.io.Serializable;
public class MyModel implements Serializable {
private String number;
public MyModel(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
package test.table;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
public class MyTableModel extends AbstractTableModel {
private Vector vector;
private String[] columnNames = { "#", "Number" };
public static final int COL_NUMBER = 0;
public static final int COL_MY_NUMBER = 1;
public MyTableModel() {
super();
vector = new Vector();
for (int i = 0; i < 5; i++) {
vector.addElement(new MyModel(Integer.toString(i)));
}
}
// This method creates a new table
public void tableChanged(ArrayList list) {
vector.clear();
for (int i = 0; i < list.size(); i++) {
MyModel element = (MyModel) list.get(i);
vector.addElement(element);
}
this.fireTableRowsInserted(0, list.size());
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
if (vector != null)
return vector.size();
return 0;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int nRow, int nCol) {
if (nRow < 0 || nRow >= getRowCount())
return "";
MyModel row = (MyModel) vector.elementAt(nRow);
switch (nCol) {
case COL_NUMBER:
return String.valueOf(nRow + 1);
case COL_MY_NUMBER:
return row.getNumber();
}
return "";
}
public void setValueAt(Object value, int nRow, int nCol) {
if (nRow < 0 || nRow >= getRowCount() || value == null)
return;
MyModel row = (MyModel) vector.elementAt(nRow);
String svalue = value.toString();
switch (nCol) {
case COL_MY_NUMBER:
row.setNumber(svalue);
break;
}
fireTableCellUpdated(nRow, nCol);
}
public Class getColumnClass(int column) {
Class dataType = getValueAt(0, column).getClass();
return dataType;
}
public boolean isCellEditable(int row, int col) {
return true;
}
}
package test.table;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;
import javax.swing.text.JTextComponent;
public class MyTable extends JTable {
public MyTable(TableModel dm) {
super(dm);
}
public MyTable(Object[][] o, String[] s) {
super(o, s);
}
public Component prepareEditor(TableCellEditor editor, int row, int
col) {
JTextComponent comp = (JTextComponent) super.prepareEditor(editor,
row,
col);
comp.setText(null);
return comp;
}
public void changeSelection(int row, int column, boolean toggle,
boolean extend) {
if (!isEditing())
super.changeSelection(row, column, toggle, extend);
}
}
Notice that when you select a row and click load, it works fine. But
when you select a particular cell and then click load. It does not
deselect the cell. It is still selected and the old value is gone. TIA.