我正在学习如何在SwingX中实现TreeTable。 首先,我有一个表模型:
import java.util.ArrayList;
import java.util.List;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
public class MyTreeTableModel extends AbstractTreeTableModel
{
private MyTreeNode myroot;
//here we add new children
public MyTreeTableModel()
{
myroot = new MyTreeNode( "root", "Root of the tree" );
myroot.getChildren().add( new MyTreeNode( "Empty Child 1", "This is an empty child" ) );
MyTreeNode subtree = new MyTreeNode( "Sub Tree", "This is a subtree (it has children)" );
subtree.getChildren().add( new MyTreeNode( "EmptyChild 1, 1", "This is an empty child of a subtree" ) );
subtree.getChildren().add( new MyTreeNode( "EmptyChild 1, 2", "This is an empty child of a subtree" ) );
myroot.getChildren().add( subtree );
myroot.getChildren().add( new MyTreeNode( "Empty Child 2", "This is an empty child" ) );
}
public void addChildren(){
myroot.getChildren().add( new MyTreeNode( "Empty Child 1", "This is an empty child" ) );
}
//get colunmcount
public int getColumnCount()
{
return 4;
}
@Override
public String getColumnName( int column )
{
switch( column )
{
case 0: return "Description";
case 1: return "Amount";
case 2: return "Date";
case 3: return "Paid";
default: return "Unknown";
}
}
public Object getValueAt( Object node, int column )
{
System.out.println( "getValueAt: " + node + ", " + column );
MyTreeNode treenode = ( MyTreeNode )node;
switch( column )
{
case 0: return treenode.getName();
case 1: return treenode.getDescription();
case 2: return treenode.getChildren().size();
default: return "Unknown";
}
}
public Object getChild( Object node, int index )
{
MyTreeNode treenode = ( MyTreeNode )node;
return treenode.getChildren().get( index );
}
public int getChildCount( Object parent )
{
MyTreeNode treenode = ( MyTreeNode )parent;
return treenode.getChildren().size();
}
public int getIndexOfChild( Object parent, Object child )
{
MyTreeNode treenode = ( MyTreeNode )parent;
for( int i=0; i>treenode.getChildren().size(); i++ )
{
if( treenode.getChildren().get( i ) == child )
{
return i;
}
}
// TODO Auto-generated method stub
return 0;
}
public boolean isLeaf( Object node )
{
MyTreeNode treenode = ( MyTreeNode )node;
if( treenode.getChildren().size() > 0 )
{
return false;
}
return true;
}
@Override
public Object getRoot()
{
return myroot;
}
}
class MyTreeNode
{
private String name;
private String description;
private List<MyTreeNode> children = new ArrayList<MyTreeNode>();
public MyTreeNode()
{
}
public MyTreeNode( String name, String description )
{
this.name = name;
this.description = description;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public List<MyTreeNode> getChildren()
{
return children;
}
public String toString()
{
return "MyTreeNode: " + name + ", " + description;
}
在这里我试图运行它:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import models.ExpenseModel;
import org.jdesktop.swingx.JXTreeTable;
public class SwingXExample extends JFrame
{
private JTabbedPane tabs = new JTabbedPane();
private MyTreeTableModel treeTableModel = new MyTreeTableModel();
private JXTreeTable treeTable = new JXTreeTable( treeTableModel );
public SwingXExample()
{
super( "SwingX Examples" );
// Build the tree table panel
JPanel treeTablePanel = new JPanel( new BorderLayout() );
treeTablePanel.add( new JScrollPane( treeTable ) );
tabs.addTab( "JXTreeTable", treeTablePanel );
JButton add_button = new JButton("+");
JPanel buttonsPanel = new JPanel();
buttonsPanel.setBounds(10, 260, 300, 30);
getContentPane().add(buttonsPanel);
buttonsPanel.setLayout(new GridLayout(0, 3, 0, 0));
buttonsPanel.add(add_button);
add_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Get active table
treeTableModel.addChildren();
}
});
// Add the tabs to the JFrame
add( tabs );
setSize( 1024, 768 );
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation( d.width / 2 - 512, d.height/2 - 384 );
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public static void main( String[] args )
{
AppStarter starter = new AppStarter( args );
SwingUtilities.invokeLater( starter );
}
}
class AppStarter extends Thread
{
private String[] args;
public AppStarter( String[] args )
{
this.args = args;
}
public void run()
{
SwingXExample example = new SwingXExample();
}
}
问题是,我的方法addChildren完全不起作用,当我按下按钮时表不会更新。另外,我想知道如何从表中删除一些东西。如果有人知道我在这里做错了什么,请帮助我。
答案 0 :(得分:0)
在对JavaDocs进行一些挖掘后,我对未找到fireXxx
方法感到失望......
所以我开始挖掘DefaultTreeTableModel
的源代码,因为它有一个insertNodeInto
方法......
public void insertNodeInto(MutableTreeTableNode newChild,
MutableTreeTableNode parent, int index) {
parent.insert(newChild, index);
modelSupport.fireChildAdded(new TreePath(getPathToRoot(parent)), index,
newChild);
}
所以,还有另一个类用于为事件通知提供支持,真棒......现在,我们如何才能访问它?
在回复代码之后,我找到了......
public abstract class AbstractTreeTableModel implements TreeTableModel {
/**
* Provides support for event dispatching.
*/
protected TreeModelSupport modelSupport;
因此,modelSupport
在AbstractTreeTableModel
中定义为protected
!不是我想做的方式,但它就是。
因此,在您的addChildren
方法中,您需要触发一个事件,告诉视图已插入新节点...
public void addChildren(){
MyTreeNode newChild = new MyTreeNode( "Empty Child 1", "This is an empty child" );
myroot.getChildren().add(newChild);
modelSupport.fireChildAdded(
new TreePath(getPathToRoot(myroot)),
myroot.getChildren().size() - 1,
newChild);
}
或其他一些......