我正在尝试使用取自http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html的示例,在该示例中,我已经用我的模型替换了文件系统模型。
我最初创建一个模型,在JTreeTable中显示它,但是现在我想更新我的模型,然后更新JTreeTable (例如,我想在树上添加一个节点,修改一个节点,删除一个节点等等)。
我不知道我怎么能做到。我看不到允许我做我想做的事情的方法,我只看到了一些方法,比如treeNodesChanged、treeNodesInserted等等,但是我可能忽略了这个JTreeTable组件的全局逻辑中的一些东西。
此外,我不确定我是否正确地创建了模型,因为在各种示例中,我看到人们在“模型”对象(model.insertNodeInto,model.reload)上调用各种方法,尽管我没有上述示例的模型object..In,但简单地称为实现TreeTableModel的抽象类AbstractTreeTableModel。
更新
public class TableModel extends AbstractTreeTableModel
implements TreeTableModel {
static protected String[] cNames = {"TrackNumber", "MWRTN", "LSRTN", "RFTN","TrackStatus","Prova","Prova2"};
// Types of the columns.
static protected Class[] cTypes = {TreeTableModel.class,Integer.class, Integer.class, Integer.class, Integer.class,String.class,String.class};
private ArrayList<Object> data=new ArrayList<Object>();
public void insertNode(Object node)
{ this.data.add(node); super.setRoot(data.get(0));}在我的主类中,我以这样的方式将对象添加到模型中:
...
model =new TableModel();
model.insertNode(threatList.get(i)); //inserting the root node
model.addChild(threatList.get(i),threatList.get(j)); // inserting the child
...然后将模型传递给我的JTreeTable并将其添加到我的框架中:
treeTable = new JTreeTable(model);
JScrollPane scroll=new JScrollPane(treeTable);
scroll.setAutoscrolls(false);
scroll.setPreferredSize(new Dimension(1000,80));
frame.add(scroll);这是JTreeTable类:
public class JTreeTable extends JTable {
protected TreeTableCellRenderer tree;
public JTreeTable(TreeTableModel treeTableModel) {
super();
// Create the tree. It will be used as a renderer and editor.
tree = new TreeTableCellRenderer(treeTableModel);
// Install a tableModel representing the visible rows in the tree.
super.setModel(new TreeTableModelAdapter(treeTableModel, tree));
// Force the JTable and JTree to share their row selection models.
tree.setSelectionModel(new DefaultTreeSelectionModel() {
// Extend the implementation of the constructor, as if:
/* public this() */ {
setSelectionModel(listSelectionModel);
}
});
// Make the tree and table row heights the same.
tree.setRowHeight(getRowHeight());
// Install the tree editor renderer and editor.
setDefaultRenderer(TreeTableModel.class, tree);
setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor());
setShowGrid(false);
setIntercellSpacing(new Dimension(0, 0));
setPreferredSize(new Dimension(60,60));
}
/* Workaround for BasicTableUI anomaly. Make sure the UI never tries to
* paint the editor. The UI currently uses different techniques to
* paint the renderers and editors and overriding setBounds() below
* is not the right thing to do for an editor. Returning -1 for the
* editing row in this case, ensures the editor is never painted.
*/
public int getEditingRow() {
return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 : editingRow;
}
//
// The renderer used to display the tree nodes, a JTree.
//
public class TreeTableCellRenderer extends JTree implements TableCellRenderer {
protected int visibleRow;
public TreeTableCellRenderer(TreeModel model) {
super(model);
}
public void setBounds(int x, int y, int w, int h) {
super.setBounds(x, 0, w, JTreeTable.this.getHeight());
}
public void paint(Graphics g) {
g.translate(0, -visibleRow * getRowHeight());
super.paint(g);
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
if(isSelected)
setBackground(table.getSelectionBackground());
else
setBackground(table.getBackground());
visibleRow = row;
return this;
}
}
//
// The editor used to interact with tree nodes, a JTree.
//
public class TreeTableCellEditor extends AbstractCellEditor implements TableCellEditor {
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int r, int c) {
return tree;
}
@Override
public Object getCellEditorValue() {
// TODO Auto-generated method stub
return null;
}
}我要做的是在添加(或修改或移除)一个子事件之后触发一个事件。
发布于 2011-08-31 08:47:54
模型是保存数据的类。每次数据更改时,它都必须告诉它的视图,以便视图刷新并显示模型的新数据。这是fireXxx()方法的目标。
与其他Swing组件一样,当您更改组件显示的数据时,您应该通过更改模型中的数据来做到这一点,并调用适当的fireXxx方法。最好的方法是将其封装到模型类中,方法是在AbstractTreeTableModel的子类中添加特定的方法,这些方法执行数据修改,并使用对fireXxx的一个或几个调用触发适当的事件。
我建议您阅读关于表格和或乔木的Swing教程,然后将这里学到的知识应用到树表中。想法是一样的。
https://stackoverflow.com/questions/7254423
复制相似问题