Witam.
Próbuje wyświetlić JTree wewnątrz JTable. Do tego celu tworze dla tabeli model i renderer:
Model
public class MyTableModel extends AbstractTableModel {
private String columnNames[] = {"c1","c2","c3","c4","c5"};
public MyTableModel() {
JTree tt = new MyTree().getTree();
cells = new Object[][] {
{tt,"2","3","4","5"}
};
}
public int getRowCount() {
return cells.length;
}
public int getColumnCount() {
return columnNames.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return cells[rowIndex][columnIndex];
}
public Class getColumnClass(int columnIndex) {
return cells[0][columnIndex].getClass();
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
}
Renderer:
public class MyTableRenderer extends JTree implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
System.out.println("renderer");
return this;
}
}
Stworzyłem sobie tez prosta klase JTree
public class MyTree extends JTree{
private JTree tree;
private DefaultMutableTreeNode root;
private DefaultMutableTreeNode node;
public MyTree() {
node = new DefaultMutableTreeNode("node");
node.add(new DefaultMutableTreeNode("node 1"));
node.add(new DefaultMutableTreeNode("node 2"));
node.add(new DefaultMutableTreeNode("node 3"));
root = new DefaultMutableTreeNode("root");
root.add(node);
tree = new JTree(root);
}
public JTree getTree() {
return this.tree;
}
}
Problem polega na tym, ze kiedy dodam MyTree do tabeli, drzewo jest wyświetlane, ale z jakimis domyślnymi wartoścami (root to JTree a niżej colors sport food). Jeżeli dodam MyTree bezpośrednio do JFrame wartości są poprawne. Dodatkowo, niemoge rozwiajc gałęzi w drzewie które znajduje sie w tabeli
Gdzie robie błąd?
PS. Kod jest przykładowy, dlatego dane sa dodawane w modelu.