0

我正在尝试理解 JXTreeTable ,因为我参考了一个站点来进行简单的 JXTreeTable 实现:下面是修改后的代码(我从一个站点中选择的)

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import org.jdesktop.swingx.JXTreeTable;

import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

class MyTreeTableModel extends AbstractTreeTableModel {

    private MyTreeNode myroot;

    public MyTreeTableModel() {
        myroot = new MyTreeNode("root", "Root of the tree", "zcxz");
        ims.CPool.configureConnPool();
        Connection con = CPool.getConnection();
        try {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select distinct category from inventory");
            Statement s2 = con.createStatement();
            ResultSet r2 = s2.executeQuery(" select category,name,supplier from inventory");
            while (rs.next()) {
                MyTreeNode cat = new MyTreeNode(rs.getString(1), " ", " ");
                myroot.getChildren().add(cat);


            }


               while(r2.next())
                  {
                      System.out.println(r2.getString(1)+"bnnbnb");
                      if(this.getValueAt( r2.getObject(1),0).equals(r2.getObject(1))==true)
                      {                              System.out.println("zxccx");


                      }
                  }
        } catch (Exception x) {
            System.out.println(x.getMessage());
            x.printStackTrace();
        }

    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Name";
            case 1:
                return "Supplier";
            case 2:
                return "Category";
            case 3:
                return "Size";

            default:
                return "Unknown";
        }
    }

    @Override
    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.getCategory();
            case 3:
                return treenode.getChildren().size();
            default:
                return "Unknown";
        }
    }

    @Override
    public Object getChild(Object node, int index) {
        MyTreeNode treenode = (MyTreeNode) node;
        return treenode.getChildren().get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        MyTreeNode treenode = (MyTreeNode) parent;
        return treenode.getChildren().size();
    }

    @Override
    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 String category;
    private List<MyTreeNode> children = new ArrayList<MyTreeNode>();

    public MyTreeNode() {
    }

    public MyTreeNode(String name, String description, String categ) {
        this.name = name;
        this.category = categ;
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String name) {
        this.category = 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 + " ," + category;
    }
}

public class TT extends JFrame {

    private JTabbedPane tabs = new JTabbedPane();
    private MyTreeTableModel treeTableModel = new MyTreeTableModel();
    private JXTreeTable treeTable = new JXTreeTable(treeTableModel);

    public TT() {
        super("SwingX Examples");

        // Build the tree table panel
        JPanel treeTablePanel = new JPanel(new BorderLayout());
        treeTablePanel.add(new JScrollPane(treeTable));
        tabs.addTab("JXTreeTable", treeTablePanel);

        // 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() {
        TT example = new TT();
    }
}

在运行代码时,我得到以下异常:

at ims.MyTreeTableModel.getValueAt(TT.java:85)
    at ims.MyTreeTableModel.<init>(TT.java:41)
    at ims.TT.<init>(TT.java:206)
    at ims.AppStarter.run(TT.java:247)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
    at java.awt.EventQueue.access$000(EventQueue.java:102)
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)

, Inevtory Items 有子类别,如查询中所示(我想生成一个 JXTreeTable,类别为 parentNodes,属于类别的 Inventory 项目为子元素)。请帮助,因为我是 JXTreeTables 的新手。

4

1 回答 1

1
class TTM extends AbstractTreeTableModel {

    TTN root;
    String[] header;

    public TTM(TTN root, String[] header) {
        this.root = root;
        this.header= header;
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }

    @Override
    public String getColumnName(int column) {
        return header[column];
    }

    @Override
    public Object getValueAt(Object node, int Column) {
        TTN treenode = (TTN) node;
        return treenode.GetValueAt(Column);
    }

    @Override
    public Object getChild(Object node, int index) {
        TTN treenode = (TTN) node;
        return treenode.GetChildren().get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        TTN treenode = (TTN) parent;
        return treenode.GetChildren().size();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        TTN treenode = (TTN) parent;
        for (int i = 0; i > treenode.GetChildren().size(); i++) {
            if (treenode.GetChildren().get(i) == child) {
                return i;
            }
        }
        return 0;
    }

    @Override
    public boolean isLeaf(Object node) {
        TTN treenode = (TTN) node;
        return treenode.GetChildren().size() <= 0;
    }

    @Override
    public Object getRoot() {
        return root;
    }
}



Class TTN extends AbstractMutableTreeTableNode {

    Object[] Row;

    public TTN() {
    }

    public TTN(Object[] Row) {
        this.Row = Row;
    }

    public Object GetValueAt(int Column) {
        return this.Row[Column];
    }

    public ArrayList<TTN> GetChildren() {
        return children;
    }

    @Override
    public String toString() {
        return this.Row[0].toString();
    }

    @Override
    public int getColumnCount() {
        return Row.length;
    }

    @Override
    public Object getValueAt(int column) {
        return Row[column];
    }
}

我在另一个网站上找到了上述课程,然后对其进行了修改。您可以将 TTN (TreeTableNode) 初始化为根节点。TableModel然后,您可以jXTreeTable1使用 的匿名新实例设置TreeTableModel。传入你初始化的根节点,以及一个String数组作为标题。

例子:

TTN root = new TTN(new Object[]{"abc", "def"});
jXTreeTable1.setTreeTableModel(new TTM(root, new String[]{"Name", "Serial", "Date added", "Limit", "Transactions type", "Type", "ID"}));

添加新行:

Node.GetChildren().add(new TTN(new Object[]{"abc", "def"}));
于 2013-12-12T12:24:18.857 回答