我尝试在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
}
};