当前位置: 技术问答>java相关
当Jtree.setEditable(true)后修改了节点的title,按回车,这时候触发了什么事件
来源: 互联网 发布时间:2015-06-27
本文导语: 当Jtree.setEditable(true)后修改了节点的title,按回车,这时候要做一些处理,怎么办,触发了什么事件吗? | 我现在找到一种方法可以试试: 修改后安回车肯定会触发改编辑器(JTextField的子类见De...
当Jtree.setEditable(true)后修改了节点的title,按回车,这时候要做一些处理,怎么办,触发了什么事件吗?
|
我现在找到一种方法可以试试:
修改后安回车肯定会触发改编辑器(JTextField的子类见DefaultTreeCellEditor)的ActionListener的方法,但如何得到改编辑器呢?
继承DefaultTreeCellEditor,重载createTreeCellEditor方法
再通过tree的setCellEditor()将你的CellEditor设置进去。大概是:
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) {
System.out.println(test.getText());
}
});
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;
}
});
修改后安回车肯定会触发改编辑器(JTextField的子类见DefaultTreeCellEditor)的ActionListener的方法,但如何得到改编辑器呢?
继承DefaultTreeCellEditor,重载createTreeCellEditor方法
再通过tree的setCellEditor()将你的CellEditor设置进去。大概是:
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) {
System.out.println(test.getText());
}
});
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;
}
});