-
Bug
-
Resolution: Fixed
-
P3
-
1.1.4
-
swing1.0fcs
-
x86
-
solaris_2.6
Name: dgC58589 Date: 11/26/97
After I select a row of the JTable and whant
it later to clear the selection of the JTable,
the JTable doesn't update itself properly.
The selection is removed (checked with
getSelectedRow() but the gui still shows a
a selection.
This happens with single selection and also
with mulitiple selections. With CellRenderer and
without CellRenderer.
Here is a code example... press the button to
clear the selection.
import com.sun.java.swing.*;
import com.sun.java.swing.table.*;
//import java.awt.swing.*;
//import java.awt.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class test
{
JTable table;
test()
{
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new GridLayout(2,1));
DefaultTableModel model = new DefaultTableModel(0,3);
model.addColumn(new Integer(0),Software.getVector());
table = new JTable(model);
table.setEmptySelectionAllowed(true);
table.setMultipleSelectionAllowed(true);
table.getColumn(new Integer(0)).setCellRenderer(new ACMCellRenderer());
frame.getContentPane().add(table);
frame.setSize(200,150);
Button btn = new Button("clear Selection");
btn.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("selected was " + table.getSelectedRow());
// first try...
table.clearSelection();
// second try
table.getSelectionModel().clearSelection();
System.out.println("selection should be cleared");
System.out.println("selected is " + table.getSelectedRow());
// workaround will repaint table
//table.repaint();
}
});
frame.getContentPane().add(btn);
frame.show();
}
public static void main(String args[])
{
new Software("test1");
new Software("test2");
new Software("test3");
new test();
}
}
class ACMCellRenderer extends JLabel implements TableCellRenderer
{
public ACMCellRenderer()
{
this.setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
Object columnID, int row)
{
ImageIcon icon = new ImageIcon(getClass().getResource("Profile.gif"));
this.setIcon(icon);
this.setText((String)value);
if (isSelected)
{
this.setBackground(new Color(0,0,128));
this.setForeground(Color.white);
}
else
{
this.setBackground(Color.white);
this.setForeground(Color.black);
}
int height = this.getIcon().getIconHeight();
if (height < 1)
height = 1;
/* if this line is commented out the CellRenderer is not called in a loop*/
//table.setRowHeight(height);
return this;
}
}
class Software
{
private static Vector softwares = new Vector();
Software(String name)
{
softwares.addElement(name);
}
public String toString(int index)
{
return (String)softwares.elementAt(index);
}
public static Vector getVector()
{
return softwares;
}
}
(Review ID: 20966)
======================================================================