JTreeTable更新
我正在尝试使用取自 http:// java.sun.com/products/jfc/tsc/articles/treetable2/index.html,其中我用我的模型替换了文件系统模型。
我最初创建一个模型,将其显示在 JTreeTable
中,但现在我想更新我的模型,然后更新 JTreeTable
(例如我想添加一个节点在树上,修改节点、删除节点等)。
我不知道我该怎么做。我看不到允许我做我想做的事情的方法,我只看到一些方法,如 treeNodesChanged
、treeNodesInserted
等,但可能我错过了全局中的一些东西此 JTreeTable
组件的逻辑。
此外,我不确定我是否正确创建了模型,因为在各种示例中,我看到人们通过“模型”对象调用各种方法(model.insertNodeInto
,model.reload< /code>),尽管我没有模型对象。在上面的示例中,它被简单地称为抽象类
AbstractTreeTableModel
,它实现了 TreeTableModel
..
更新
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;
}
}
我要做的是触发一个事件添加(或修改或删除)子项后。
I'm trying to use the example taken from the http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html, in which I've substituted the filesystem model with my model.
I initially create a model, I display it in the JTreeTable
, but now I'd like to update my model and then the JTreeTable
(for example I want to add a node at the tree, modify a node, remove a node, etc.).
I don't know how I can do it. I can't see method that allow me to do what I want, I only see some method like treeNodesChanged
, treeNodesInserted
, etc., but probably I miss something in the global logic of this JTreeTable
component.
Besides I'm not sure that I correctly create the model, because in various example I've seen people call various method over a "model" object (model.insertNodeInto
, model.reload
), inspite of I haven't a model object..In the example above, is simply called the abstract class AbstractTreeTableModel
which implements TreeTableModel
..
Update
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));}
In my main class I add objects to my model in this way:
...
model =new TableModel();
model.insertNode(threatList.get(i)); //inserting the root node
model.addChild(threatList.get(i),threatList.get(j)); // inserting the child
...
Then I pass the model to my JTreeTable
and add it to my frame:
treeTable = new JTreeTable(model);
JScrollPane scroll=new JScrollPane(treeTable);
scroll.setAutoscrolls(false);
scroll.setPreferredSize(new Dimension(1000,80));
frame.add(scroll);
And this is the JTreeTable class:
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;
}
}
What I would is to fire an event after adding(or modifying, or removing) a child.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模型是保存数据的类。每次数据发生变化时,它都必须告诉视图,以便视图自行刷新并显示模型的新数据。这是
fireXxx()
方法的目标。与其他 Swing 组件一样,当您更改组件显示的数据时,您应该通过更改模型中的数据来实现,并调用适当的 fireXxx 方法。最好的做法是将其封装在模型类中,方法是在 AbstractTreeTableModel 的子类中添加特定方法,这些方法执行数据修改并使用一次或多次调用
fireXxx
来触发适当的事件。我建议您阅读有关 表 的 Swing 教程,或者 trees ,然后将您在此处学到的内容应用到您的树表中。这个想法是一样的。
The model is the class holding your data. It must tell its view each time the data changes, so that the view refreshes itself and shows the new data of the model. This is the goal of the methods
fireXxx()
.Like with the other Swing components, when you change the data displayed by the component, you should thus do it by changing the data in the model, and call the appropriate fireXxx methods. The best thing to do is to encapsulate this in the model class, by adding specific methods in your subclass of AbstractTreeTableModel which perform the data modification and fire the appropriate event using one or several calls to
fireXxx
.I suggest you read the Swing tutorial about tables and or trees and then apply what you learn here to your tree table. The idea is the same.