Pessoal, estou usando um JTable associado a um TableModel que implementei para manipular um arquivo XML.
A leitura e gravação no arquivo XML e carregamento no JTable está OK, porém, quando altero um campo ou adiciono
uma linha no XML, os dados não são atualizados automaticamente no JTable, somente depois que eu redimensiono
a minha janela. No meu TableModel eu chamo os métodos fireTableRowsInserted e fireTableCellUpdated mas não
está fazendo efeito.
Abaixo um trecho de código das minhas classes.
public class XmlDataModel extends AbstractTableModel {
public void addRow(String description, String fileName) {
Element parent = doc.getDocumentElement();
Element el = doc.createElement(ROOT_ELEMENT_TAG);
el.setAttribute(colNames[descriptionCol], description);
el.setAttribute(colNames[fileNameCol], fileName);
parent.appendChild(el);
int lines = getRowCount()-1;
fireTableRowsInserted(lines, lines);
}
public boolean isCellEditable(int r, int c) {
return false;
}
public void setValueAt(Object value, int r, int c) {
NodeList nl = doc.getElementsByTagName(ROOT_ELEMENT_TAG);
NamedNodeMap atribs = nl.item(r).getAttributes();
Node node = atribs.getNamedItem(colNames[c]);
node.setNodeValue(value.toString());
atribs.setNamedItem(node);
fireTableCellUpdated(r,c);
}
}
public class MainForm extends JFrame {
private JTable tb;
private XmlDataModel dataModel;
public MainForm() {
dataModel = new XmlDataModel(xmlFileName);
tb = new JTable(dataModel);
cp.add(new JScrollPane(tb));
btnAdd = new JButton("Add row");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dataModel.addRow("description", "name");
}
});
}
}