我正在尝试创建一个应用程序,该应用程序允许编辑嵌套在JTree中作为节点的JTable。该应用程序非常简单。 我已正确添加了JTable:添加“步骤”后,双击白色栏以展开JTree的根节点。
但是,我不知道从哪里开始实际编辑作为JTree节点的JTable。
我该怎么做?我已经看到了相关问题的一些链接,但是我不知道如何开始使用它们,尽管被认为是正确的,但所有问题都没有明确的答案。
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class TP
{
Random seed;
private DefaultMutableTreeNode selectedNode;
private String[] REAGENT_CNAMES = {"Name", "Quantity", "Quantity Type"};
private String[] STEP_CNAMES = {"Name", "Time", "Time Units"};
public TP()
{
seed = new Random();
// south section
JButton addButton = new JButton("Add Step");
addButton.setEnabled(true);
// center section
JTree tree = createTree();
tree.setCellRenderer(new PanelRenderer());
tree.getSelectionModel().addTreeSelectionListener(
new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent e)
{
Object lastPathComponent = e.getPath().getLastPathComponent();
TP.this.selectedNode = (DefaultMutableTreeNode)lastPathComponent;
System.out.println("Number of rows: " + ((JTable)TP.this.selectedNode.getUserObject()).getRowCount());
System.out.println("Level of the selected node is " + TP.this.selectedNode.getLevel());
switch(TP.this.selectedNode.getLevel()){
case 0:
addButton.setText("Add Step");
break;
case 1:
addButton.setText("Add Reagent");
break;
case 2:
addButton.setText("Add Reagent");
break;
}
addButton.setEnabled(true);
}
});
tree.setRowHeight(0);
JFrame f = new JFrame();
f.getContentPane().setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(tree), BorderLayout.CENTER);
JPanel addPanel = new JPanel(new FlowLayout());
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
DefaultTreeModel dtm = (DefaultTreeModel)(tree.getModel());
DefaultMutableTreeNode parent = TP.this.selectedNode;
DefaultMutableTreeNode toAdd;
DefaultTableModel tableModel;
JTable newTable;
String[][] defaultStepData = {{"Timepoint", "4", "hrs"}};
String[][] blankData = {{"\t","\t","Default"}};
switch(parent.getLevel()){
case 0:
tableModel = new DefaultTableModel(defaultStepData, STEP_CNAMES);
newTable = new JTable(tableModel);
JComboBox comboBox = new JComboBox();
comboBox.addItem("days");
comboBox.addItem("hours");
comboBox.addItem("minutes");
newTable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(comboBox));
toAdd = new DefaultMutableTreeNode(newTable);
dtm.insertNodeInto(toAdd, parent, parent.getChildCount());
break;
case 1:
tableModel = new DefaultTableModel(blankData, REAGENT_CNAMES);
newTable = new JTable(tableModel);
toAdd = new DefaultMutableTreeNode(newTable);
dtm.insertNodeInto(toAdd, parent, parent.getChildCount());
break;
case 2:
JTable myTable = (JTable)(parent.getUserObject());
DefaultTableModel theTableModel = (DefaultTableModel)(myTable.getModel());
theTableModel.addRow(blankData);
dtm.nodeChanged(parent);
break;
default:
break;
}
}
});
addPanel.add(addButton);
f.add(addPanel, BorderLayout.SOUTH);
f.setSize(1000,800);
f.setLocation(200,200);
f.setVisible(true);
}
private JTree createTree()
{
int children = 0;
int grandChildren = 0;
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JTable());
TP.this.selectedNode = root;
DefaultMutableTreeNode node;
for(int j = 0; j < children; j++)
{
String[][] pdata = {{"Jonah","4", "Rules"}};
String[] pcnames = {"Name", "Time", "Time Units"};
JTable parent = new JTable(pdata, pcnames);
node = new DefaultMutableTreeNode(parent);
root.add(node);
for(int k = 0; k < grandChildren; k++){
String[][] data = {{"A11", "A12", "A13"}, {"A21", "A22", "A23"}, {"A31", "A32", "A33"}};
String[] cnames = {"Col1", "Col2", "Col3"};
JTable value = new JTable(data, cnames);
node.add(new DefaultMutableTreeNode(value));
}
}
DefaultTreeModel model = new DefaultTreeModel(root);
JTree treeToReturn = new JTree(model);
return treeToReturn;
}
public static void main(String[] args)
{
new TP();
}
}
class PanelRenderer implements TreeCellRenderer
{
public PanelRenderer(){}
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
{
JTable tbl = (JTable)(((DefaultMutableTreeNode)value).getUserObject());
JScrollPane jsp = new JScrollPane(tbl);
if(tbl.getRowCount() == 0){
jsp.setPreferredSize(new Dimension(500, 25));
}else{
Dimension prefSize = tbl.getPreferredSize();
jsp.setPreferredSize(new Dimension((int)(prefSize.getWidth()), (int)(prefSize.getHeight())+20));
}
if(hasFocus){
jsp.setBackground(Color.BLUE);
}
//jsp.setSize(500, tbl.getRowCount()*30 + 25);
System.out.println("Row "+row+" " + tbl.getPreferredSize());
return jsp;
}
}