swing jxtreetable列中的数据

643ylb08  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(199)

我对swing和swingx完全陌生,我想创建一个类似于javafx中treetableview的树表视图。通过一系列的搜索,我找到了一个我一直在研究的例子。下面是示例代码:

package test2;

import org.jdesktop.swingx.JXTreeTable;

import org.jdesktop.swingx.decorator.AbstractHighlighter;

import org.jdesktop.swingx.decorator.ComponentAdapter;

import org.jdesktop.swingx.decorator.Highlighter;

import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;

import org.jdesktop.swingx.treetable.DefaultTreeTableModel;

import org.jdesktop.swingx.treetable.TreeTableModel;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.Arrays;

public class TreeTableDemo2 implements Runnable {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TreeTableDemo2());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Tree Table Demo");
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(createPanel(), BorderLayout.CENTER);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * @wbp.parser.entryPoint
     */
    public JPanel createPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        TreeTableModel treeTableModel = new DummyTreeTableModel();
        JXTreeTable treeTable = new JXTreeTable(treeTableModel);
        JScrollPane treeTableScroll = new JScrollPane(treeTable);

        panel.add(treeTableScroll, BorderLayout.CENTER);
        return panel;
    }

    private static class DummyTreeTableNode extends DefaultMutableTreeTableNode {
        private final Object[] values;

        private DummyTreeTableNode(String name) {
            super(name);
            values = new Object[5];
            values[0] = name;
        }

        private DummyTreeTableNode(Object... values) {
            super(values[0]);
            this.values = values;
        }

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

    private static class DummyTreeTableModel extends DefaultTreeTableModel {
        private static DefaultMutableTreeTableNode rootNode = new DefaultMutableTreeTableNode();
        static {

            DefaultMutableTreeTableNode blue = new DefaultMutableTreeTableNode("Blue");
            blue.add(new DummyTreeTableNode("Orionis C",          33000,  30000.0,    18.0,   5.90));
            rootNode.add(blue);

            DefaultMutableTreeTableNode bluish = new DefaultMutableTreeTableNode("Bluish");
            bluish.add(new DummyTreeTableNode("Becrux",             30000,  16000.0,    16.0,   5.70));
            bluish.add(new DummyTreeTableNode("Spica",              22000,  8300.0,     10.5,   5.10));
            bluish.add(new DummyTreeTableNode("Achernar",           15000,  750.0,      5.40,   3.70));
            bluish.add(new DummyTreeTableNode("Rigel",              12500,  130.0,      3.50,   2.70));
            rootNode.add(bluish);

            DefaultMutableTreeTableNode blueWhite = new DefaultMutableTreeTableNode("Blue-White");
            blueWhite.add(new DummyTreeTableNode("Sirius A",           9500,   63.0,       2.60,   2.30));
            blueWhite.add(new DummyTreeTableNode("Fomalhaut",          9000,   40.0,       2.20,   2.00));
            blueWhite.add(new DummyTreeTableNode("Altair",             8700,   24.0,       1.90,   1.80));
            rootNode.add(blueWhite);

            DefaultMutableTreeTableNode white = new DefaultMutableTreeTableNode("White");
            white.add(new DummyTreeTableNode("Polaris A",          7400,   9.0,        1.60,   1.50));
            white.add(new DummyTreeTableNode("Eta Scorpii",        7100,   6.3,        1.50,   1.30));
            white.add(new DummyTreeTableNode("Procyon A",          6400,   4.0,        1.35,   1.20));
            rootNode.add(white);

            DefaultMutableTreeTableNode yellowWhite = new DefaultMutableTreeTableNode("Yellow-White");
            yellowWhite.add(new DummyTreeTableNode("Alpha Centauri A",   5900,   1.45,       1.08,   1.05));
            yellowWhite.add(new DummyTreeTableNode("The Sun",            5800,   100.0,      1.00,   1.00));
            yellowWhite.add(new DummyTreeTableNode("Mu Cassiopeiae",     5600,   0.70,       0.95,   0.91));
            yellowWhite.add(new DummyTreeTableNode("Tau Ceti",           5300,   0.44,       0.85,   0.87));
            rootNode.add(yellowWhite);

            DefaultMutableTreeTableNode orange = new DefaultMutableTreeTableNode("Orange");
            orange.add(new DummyTreeTableNode("Pollux",             5100,   0.36,       0.83,   0.83));
            orange.add(new DummyTreeTableNode("Epsilon Eridani",    4830,   0.28,       0.78,   0.79));
            orange.add(new DummyTreeTableNode("Alpha Centauri B",   4370,   0.18,       0.68,   0.74));
            rootNode.add(orange);

            DefaultMutableTreeTableNode red = new DefaultMutableTreeTableNode("Red");
            red.add(new DummyTreeTableNode("Lalande 21185",      3400,   0.03,       0.33,   0.36));
            red.add(new DummyTreeTableNode("Ross 128",           3200,   0.0005,     0.20,   0.21));
            red.add(new DummyTreeTableNode("Wolf 359",           3000,   0.0002,     0.10,   0.12));
            rootNode.add(red);
        }

        private static final Object[] columnNames = {
                "File Name", "Object", "Field Name", "File Type", "Object Type", "Database Accesses"
        };

        public DummyTreeTableModel() {
            super(rootNode, Arrays.asList(columnNames));
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return String.class;
            } else {
                return Double.class;
            }
        }

        @Override
        public boolean isCellEditable(Object node, int column) {
            return false;
        }
    }
}

这正是我想要的(我已经更改了列名,但这并不重要),我的问题是如何将数据放入其他列中?展开时,我希望能够显示列2、3、4、5等中的数据。。
有人能给我指出正确的方向吗?就像我说的,我是一个全新的摇摆者,如果这个问题看起来有点愚蠢,请原谅我。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题