JTree节点上JTable的问题

时间:2015-08-19 01:31:09

标签: java jtable jtree

我尝试在JTable的节点上创建JTree。我在以下实现中遇到了几个问题:

  • 由于某种原因,用户无法与JTree进行互动(选择单元格)。
  • 如果我将JTable放在JScrollPane中,则滚动不起作用。 JTree我实施的JScrollPane也在JScrollPane中,因此可能存在一些干扰(#scrollception)。
  • 我无法自定义表格的尺寸
  • 只有在import java.awt.Component; import java.util.HashMap; import java.util.Map; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTree; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import javax.swing.tree.DefaultTreeCellRenderer; public class ExampleRenderer extends DefaultTreeCellRenderer { private Map<String, JTable> tables = new HashMap<String, JTable>(); @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { Component c = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); if (value.toString().startsWith("TABLE:")) { c = tables.get(value.toString()); if (c == null) { JTable table = new JTable(createModel(value.toString())); //table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); //JScrollPane pane = new JScrollPane(table); tables.put(value.toString(), table); c = tables.get(value.toString()); } } return c; } private TableModel createModel(String tableData) { tableData = tableData.substring(6, tableData.length()); DefaultTableModel model = new DefaultTableModel(); String[] colData = tableData.split("¬"); for (String data : colData) { String[] components = data.split("="); model.addColumn(components[0], components[1].split(",")); } return model; } }
  • 中实施时,才会显示表格的标题

请帮助我解决这些问题。

节点中的JTable

public ExampleJTreeWithJTable() {
    setLayout(new BorderLayout(0, 0));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Example");
    root.add(new DefaultMutableTreeNode("TABLE:Column A=Cell 1.1,Cell 2.1,Cell 3.1¬Column B=Cell 1.2,Cell 2.2,Cell 3.2"));

    JTree tree = new JTree(root);
    tree.setCellRenderer(new ExampleRenderer());

    add(tree, BorderLayout.CENTER);
    pack();
}

示例实施

var MyObject = {

 init: function() {
   this.element = $('.someElement');


   this.element.bind('click', this._doSomething.bind(this));
 },

 _doSomething = function() {
    // METHOD WHICH I WILL MANIPULATE THIS.ELEMENT

 }

};

0 个答案:

没有答案