An example of code to reproduce the bug is:
(before we must expand the row and after uncheck the checkBox)
- Code: Select all
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import com.jidesoft.grid.*;
public class HierarchicalTest {
static String[]colNames = {"col1","col2"};
static String[][]tabledata = {{"elem11","elem12"}};
static String[][]tableData1 = {{"subTable11","subTable12"}};
MyTableModel model;
HierarchicalTable mainTable;
public HierarchicalTest() {
model = new MyTableModel(tabledata,colNames);
mainTable = new HierarchicalTable(model);
mainTable.setComponentFactory(new HierCompFactory());
JPanel contP = new JPanel(new BorderLayout());
contP.add(new JScrollPane(mainTable),BorderLayout.CENTER);
JCheckBox cb = new JCheckBox("isHierarchical");
cb.setSelected(true);
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.haschild = ((JCheckBox)e.getSource()).isSelected();
model.fireTableDataChanged();
}});
contP.add(cb,BorderLayout.SOUTH);
JFrame d = new JFrame();
d.setContentPane(contP);
d.pack();
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.setVisible(true);
}
class MyTableModel extends DefaultTableModel implements HierarchicalTableModel{
boolean haschild = true;
MyTableModel childModel;
public MyTableModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}
public boolean hasChild(int arg0) {
return haschild;
}
public boolean isHierarchical(int arg0) {
return true;
}
public Object getChildValueAt(int arg0) {
if(childModel == null) {
childModel = new MyTableModel(tableData1,colNames);
childModel.haschild = false;
}
return childModel;
}
public boolean isExpandable(int arg0) {
return true;
}
}
class HierCompFactory implements HierarchicalTableComponentFactory{
public Component createChildComponent(HierarchicalTable arg0, Object arg1, int arg2) {
JPanel p = new JPanel(new BorderLayout());
p.add(new JTable((TableModel) arg1),BorderLayout.CENTER);
return new HierarchicalPanel(p);
}
public void destroyChildComponent(HierarchicalTable arg0, Component arg1, int arg2) {}
}
public static void main(String[] args) {
new HierarchicalTest();
}
}
There is a way to fix this problem and automatically collapse the expanded row?