我想要发生的是在编辑 JTree 时,如果我在编辑框外单击,我想提交编辑。
tree.setInvokesStopCellEditing(true);
有所帮助,如果我单击 JTree 中的某处,就会提交编辑。但是,如果我在 JTree 外部单击,比如 JTextPane,则编辑不会提交。因此问题:问题:当焦点丢失时,如何让 JTree 停止单元格编辑?或者在 JTree 位置外左键单击时?还有一种首选方法吗?
注意:在下面的代码中,我尝试使用 Focus Listener Annoymous 内部类来解决这个问题,并尝试了伪代码 WhenTreeLosesFocus() { if( tree.isEditing() ) tree.stopEditing(); }
这不起作用,因为在编辑时,焦点更改为树中的 CellEditor 组件,我看到编辑框闪烁,然后快速闪烁。
解决方案不必基于 Focus,可以在 JTree 区域外左键单击。
紧随其后的是 SSCE(顺便说一句,如果您运行 Convienence 的代码,空格键和 F2 都会重命名节点。)
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.KeyStroke;
public class StackExchangeQuestion3 {
public static void main(String[] args){
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle("Stack Exchange Question");
window.setSize(400,500);//variable parameters would be best
window.setVisible(true);
JSplitPane split = new JSplitPane();
window.getRootPane().setContentPane(split);
JScrollPane left = new JScrollPane();
JScrollPane right = new JScrollPane();
JTree tree = new JTree();//loads sample tree data
tree.setEditable(true);//select a node then press F2 to edit (built in keybinding)
tree.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "startEditing");//can edit with space now
tree.setInvokesStopCellEditing(true);//this helps stop editing within focus of tree
JTextPane text = new JTextPane();
split.setLeftComponent(left);
split.setRightComponent(right);
left.setViewportView(tree);
right.setViewportView(text);
split.setDividerLocation(200);
tree.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) { }
public void focusLost(FocusEvent e) {
JTree tree = (JTree)e.getSource();
if( tree.isEditing() ) tree.stopEditing();
}
});//end addFocusListener
}//end main
}
欢迎对是否一种方式优于另一种方式的推理进行评论。 以及有关如何检测鼠标是否在 JTree 外部单击的提示/正确方向的提示。
最佳答案
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
public class StackExchangeQuestion3AnsA {
public static void main(String[] args){
new StackExchangeQuestion3AnsA();
}
StackExchangeQuestion3AnsA(){
JFrame window;
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle("Stack Exchange Answer");
window.setSize(400,500);//variable parameters would be best
window.setVisible(true);
JSplitPane split = new JSplitPane();
window.setContentPane(split);
JScrollPane left = new JScrollPane();
JScrollPane right = new JScrollPane();
JTree tree;
tree = new JTree();//loads sample tree data
tree.setEditable(true);//select a node then press F2 to edit (built in keybinding)
tree.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "startEditing");//can edit with space now
tree.setInvokesStopCellEditing(true);//this helps stop editing within focus of tree
//even with my fix the above line is still needed
JTextPane text = new JTextPane();
split.setLeftComponent(left);
split.setRightComponent(right);
left.setViewportView(tree);
right.setViewportView(text);
split.setDividerLocation(200);
tree.setCellEditor( new MyTreeCellEditor(tree, tree.getCellRenderer()) );
}//end constructor
private class MyTreeCellEditor extends DefaultTreeCellEditor {
public MyTreeCellEditor(JTree tree, TreeCellRenderer renderer) {
super(tree, (DefaultTreeCellRenderer)renderer);
//Note: http://stackoverflow.com/questions/5031101/why-i-lose-the-focus-for-a-short-time-from-jtree-after-editing-a-node?rq=1
//André mentions "The tree adds a DefaultCellEditor to the JTree hierarchy when editing starts. This textfield gains the focus."
//it's not as simple as just this.addFocusListener()
//2 variables: editingComponent and editingContainer, are inherited from DefaultTreeCellEditor
//the constructor doesn't initialize the editingComponent
//it's null atm, so we can't add focusListener to it, (yet at a later time it will gain focus)
//FocusOwner (on edit): javax.swing.tree.DefaultTreeCellEditor$DefaultTextField
//editingComponent is by default a DefaultTextField
}
@Override
public Component getTreeCellEditorComponent(final JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
Component container = super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
//Note: System.out.println("Components Type: "+containeractually.getClass().getName());
//was used to show it was container, javax.swing.tree.DefaultTreeCellEditor$EditorContainer
//getTreeCellEditorComponent(parameters) is called as soon as editing begins
//also at this time editingComponent != null (aka initialized)
//so it's a good place to add in a Focus Listener
editingComponent.addFocusListener( new FocusListener(){
@Override public void focusGained(FocusEvent e) { }
@Override public void focusLost(FocusEvent e) {
tree.stopEditing();} } );
//EditorContainer is responsible for displaying the editingComponent
//so we added focusListener, after editingComponent initialized, and before it's used
//(I think the return statement means it's about to be used)
return container;
}
}//end MyTreeCellEditor
}//end class
关于java - 当在 JTree Location 之外发生 Focus Lost 或 Left Click 时,如何使 JTree 停止单元格编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863179/