I'm developing a Desktop Application in java(jdk 1.6) with swing but having a problem after refreshing the view. Whenever I update the Model of a JTable, I see that the data in this model changes but a problem in the view occurs.
It only overwrites the old data in the cells with the new data and blurs the data in the cells, as if it puts one data picture on top of other. When I double click to the cells, I can read the new data. (can't post an image unfortunately)
I tried
- to set datamodel at first to null,call repaint() then loading the new model and repaint()
- to call fireTableDataChanged()
- to search similar problems and questions
Nothing worked. I have many tables like that but only this table has such a problem.
To update underlying data model:
void loadTask(Task t) {
(....)
TaskPropertiesModel = new DefaultTableModel();
Object[][] data = new Object[][]
{
{"Start :", t.getStart()},
{"End :", t.getEnd()},
{"Freq. :", t.getFrequency()},
{"Roles :", roles},
{"Hard :", t.getHard()},
{"Values:", values}
};
TaskPropertiesModel.setDataVector(data, new Object[2]); //no column is shown
}
Table Properties:
TaskPropertiesTable.setMaximumSize(new java.awt.Dimension(150, 90));
TaskPropertiesTable.setMinimumSize(new java.awt.Dimension(150, 90));
TaskPropertiesTable.setRowSelectionAllowed(false);
TaskPropertiesTable.getTableHeader().setResizingAllowed(false);
TaskPropertiesTable.getTableHeader().setReorderingAllowed(false);
TaskPropertiesTable.setTableHeader(null);
TaskPropertiesTable.setPreferredScrollableViewportSize(TaskPropertiesTable.getPreferredSize());
TaskPropertiesTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [6][2],new String [2] ));
TaskPropertiesTable.setDefaultRenderer(Object.class, new MultiLineCellRenderer());
MultiLineCellRenderer:
public class MultiLineCellRenderer extends JTextArea implements TableCellRenderer {
public MultiLineCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(Color.BLACK);
setBackground(Color.LIGHT_GRAY);
}
else {
setForeground(Color.BLACK);
setBackground(Color.WHITE);
}
setFont(table.getFont());
if (hasFocus) {
setBorder(new FocusBorder(Color.RED, Color.RED));
if (table.isCellEditable(row, column)) {
setForeground(Color.BLACK);
setBackground(Color.LIGHT_GRAY);
}
} else {
setBorder(new BevelBorder(1)); // This was emptyBorder.
//after changing it, the problem is solved
}
if (!table.isCellEditable(row, column)) {
setForeground(Color.BLACK);
setBackground(Color.LIGHT_GRAY);
}
setText((value == null) ? "" : value.toString());
//---setting the row height--//
}
Thanks in advance
Isil