当前位置: 技术问答>java相关
当Jtree.setEditable(true)后修改了节点的title,按回车,做些判断后怎么让修改失效,改回原来的title(gularess(无吃哪拉)再帮帮忙)
来源: 互联网 发布时间:2015-06-28
本文导语: 我先记下了修改前的title,String oldtitle tree.setCellEditor(new DefaultTreeCellEditor(tree,(DefaultTreeCellRenderer)tree.getCellRenderer()) { protected TreeCellEditor createTreeCellEditor() { javax.swing.border.Border aBorder = UIManager....
我先记下了修改前的title,String oldtitle
tree.setCellEditor(new DefaultTreeCellEditor(tree,(DefaultTreeCellRenderer)tree.getCellRenderer()) {
protected TreeCellEditor createTreeCellEditor() {
javax.swing.border.Border aBorder = UIManager.getBorder("Tree.editorBorder");
final DefaultTextField test = new DefaultTextField(aBorder);
test.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (操作数据库失败)
test.setText(oldTitle);
}
});
DefaultCellEditor editor = new DefaultCellEditor
(test) {
public boolean shouldSelectCell(java.util.EventObject event) {
boolean retValue = super.shouldSelectCell(event);
getComponent().requestFocus();
return retValue;
}
};
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}
});
上面这样做不对,问问应该怎么做,谢谢
tree.setCellEditor(new DefaultTreeCellEditor(tree,(DefaultTreeCellRenderer)tree.getCellRenderer()) {
protected TreeCellEditor createTreeCellEditor() {
javax.swing.border.Border aBorder = UIManager.getBorder("Tree.editorBorder");
final DefaultTextField test = new DefaultTextField(aBorder);
test.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (操作数据库失败)
test.setText(oldTitle);
}
});
DefaultCellEditor editor = new DefaultCellEditor
(test) {
public boolean shouldSelectCell(java.util.EventObject event) {
boolean retValue = super.shouldSelectCell(event);
getComponent().requestFocus();
return retValue;
}
};
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}
});
上面这样做不对,问问应该怎么做,谢谢
|
因为编辑是在你双击树结点后发生的,tree.addMouseListener,把相应的选中title记住就行了!如果失败,就设回来!