gpt4 book ai didi

java - TreeCellEditor 在编辑单元格后执行操作

转载 作者:太空宇宙 更新时间:2023-11-04 13:41:24 25 4
gpt4 key购买 nike

我想在 CellEditor 消失后执行一个操作。之后调用哪个方法,从哪里开始?

最佳答案

您必须通过向树添加 CellEditorListener 来监听 JTree 的 CellEditor

 tree.getCellEditor().addCellEditorListener(new CellEditorListener() {

@Override
public void editingStopped(ChangeEvent e) {
System.out.println(((DefaultCellEditor)e.getSource()).getCellEditorValue());
}

@Override
public void editingCanceled(ChangeEvent e) {
System.out.println(((DefaultCellEditor)e.getSource()).getCellEditorValue());
}
});

完整示例

  JFrame frame = new JFrame();

//Mock data
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
for (int i = 1; i < 5; i++) {
DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Parent" + i);
for (int j = 1; j < 5; j++) {
parent.add(new DefaultMutableTreeNode("Child" + j));
}
root.add(parent);
}

DefaultTreeModel tmodel = new DefaultTreeModel(root);
JTree tree = new JTree(tmodel);

tree.setEditable(true);//To enable editing

tree.getCellEditor().addCellEditorListener(new CellEditorListener() {

@Override
public void editingStopped(ChangeEvent e) {
//add you code here
System.out.println(((DefaultCellEditor) e.getSource()).getCellEditorValue());
}

@Override
public void editingCanceled(ChangeEvent e) {
//add you code here too
System.out.println(((DefaultCellEditor) e.getSource()).getCellEditorValue());
}
});
frame.add(new JScrollPane(tree));

frame.setTitle("Tree Editor Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

关于java - TreeCellEditor 在编辑单元格后执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31230491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com