-
Bug
-
Resolution: Duplicate
-
P1
-
None
-
1.4.0
-
sparc
-
solaris_8
invoking fireTableDataChanged() in the overrided TableModel.setValueAt() makes the selected table rows be cleared in jdk1.4, but in jdk1.2 it does not.
compile and run the following codes, under jdk1.4, editing the first column, you can find after we change value of the first column, the selection will be cleared.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.table.TableColumn;
import java.awt.event.*;
public class TestJTbl extends JFrame
{
JButton b, b1;
int col = 4;
TableColumn columns[];
JTable table;
public TestJTbl()
{
String[] choice = {"Mark", "Tom", "Alan", "Jeff", "Amy"};
table = new JTable(new MyModel());
TableColumn column = table.getColumnModel().getColumn(0);
JComboBox combo = new JComboBox();
for (int i = 0; i < choice.length; i++) {
combo.addItem(choice[i]);
}
column.setCellEditor(new DefaultCellEditor(combo));
table.setRowSelectionAllowed(true);
ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
colSM.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//table.setColumnSelectionAllowed(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(scrollPane, BorderLayout.CENTER);
}
static public void main(String[] args) {
TestJTbl spTest = new TestJTbl();
spTest.pack();
spTest.setVisible(true);
}
}
class MyModel extends AbstractTableModel {
String[][] data = {
{"Mark", "Andrews", "red", "Baseball", "1"},
{"Tom", "Ball", "Blue", "Football", "2"},
{"Alan", "Chung", "Green", "Baseball", "3"},
{"Jeff", "Dinkins", "Turquois", "Football", "4"},
{"Amy", "Fowler", "Yellow", "Hockey", "5"}
};
String[] headers = {"col1", "col2", "col3", "col4", "col5"};
public String getColumnName(int column) {
return headers[column];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return true;
} else {
return false;
}
}
public int getColumnCount() {
return headers.length;
}
public int getRowCount() {
return data.length;
}
public void setValueAt(Object aValue, int row, int column) {
data[row][column] = (String)aValue;
fireTableDataChanged();
}
public Object getValueAt(int row, int column) {
return data[row][column];
}
}
compile and run the following codes, under jdk1.4, editing the first column, you can find after we change value of the first column, the selection will be cleared.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.table.TableColumn;
import java.awt.event.*;
public class TestJTbl extends JFrame
{
JButton b, b1;
int col = 4;
TableColumn columns[];
JTable table;
public TestJTbl()
{
String[] choice = {"Mark", "Tom", "Alan", "Jeff", "Amy"};
table = new JTable(new MyModel());
TableColumn column = table.getColumnModel().getColumn(0);
JComboBox combo = new JComboBox();
for (int i = 0; i < choice.length; i++) {
combo.addItem(choice[i]);
}
column.setCellEditor(new DefaultCellEditor(combo));
table.setRowSelectionAllowed(true);
ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
colSM.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//table.setColumnSelectionAllowed(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(scrollPane, BorderLayout.CENTER);
}
static public void main(String[] args) {
TestJTbl spTest = new TestJTbl();
spTest.pack();
spTest.setVisible(true);
}
}
class MyModel extends AbstractTableModel {
String[][] data = {
{"Mark", "Andrews", "red", "Baseball", "1"},
{"Tom", "Ball", "Blue", "Football", "2"},
{"Alan", "Chung", "Green", "Baseball", "3"},
{"Jeff", "Dinkins", "Turquois", "Football", "4"},
{"Amy", "Fowler", "Yellow", "Hockey", "5"}
};
String[] headers = {"col1", "col2", "col3", "col4", "col5"};
public String getColumnName(int column) {
return headers[column];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return true;
} else {
return false;
}
}
public int getColumnCount() {
return headers.length;
}
public int getRowCount() {
return data.length;
}
public void setValueAt(Object aValue, int row, int column) {
data[row][column] = (String)aValue;
fireTableDataChanged();
}
public Object getValueAt(int row, int column) {
return data[row][column];
}
}
- duplicates
-
JDK-4276786 JTable looses selection when the model fires "fireTableDataChanged"
- Closed