【发布时间】:2011-11-14 03:05:29
【问题描述】:
双击时,我将双击的 Jtree 节点的背景高亮为绿色。当我双击其他节点时,之前选择的绿色节点应该会恢复到原来的状态,并且新双击的节点应该会亮起绿色。
它有点工作,但行为不一致。当我双击子节点时,它们将变为绿色,并且之前双击的节点保持绿色,直到我选择该节点并选择另一个节点....
这是我的自定义渲染器。
public class MyRenderer extends DefaultTreeCellRenderer{
public MyRenderer() {
}
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
NodePro np = (NodePro)(node.getUserObject());
if(np.getNodeancestors() != null){
if(np.getNodeancestors().contains("activated")){
setBackgroundNonSelectionColor(Color.GREEN);
}else{
setBackgroundNonSelectionColor(null);
}
}else{
setBackgroundNonSelectionColor(null);
}
return this;
}
}
这是我的 Jtree,它添加了双击监听器。
public class Tree extends JTree{
private static Tree INSTANCE;
private TreeSelectionListenerClass tsl;
private TreePopupTriggerListener ptl;
private TreeDoubleClickListener dbll;
public Tree() {
// TODO Auto-generated constructor stub
tsl = new TreeSelectionListenerClass();
dbll = new TreeDoubleClickListener();
addTreeSelectionListener(tsl);
addMouseListener(dbll);
setToggleClickCount(0);
setCellRenderer(new MyRenderer());
}
这里是我捕捉到双击的地方......
public class TreeDoubleClickListener implements MouseInputListener {
public TreeDoubleClickListener(){
}
/*On Double Click (This Node Will Be Activated), Save To Activated Node */
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2){
Global.showWaitCursor();
try{
DefaultMutableTreeNode selected = (DefaultMutableTreeNode) Global.previousTreePath.getLastPathComponent();
NodePro npx = (NodePro) selected.getUserObject();
npx.setNodeancestors("");
TreePath path = Tree.getInstance().getPathForLocation(e.getX(), e.getY());
if (path != null){
Global.previousTreePath = path;
DefaultMutableTreeNode current = (DefaultMutableTreeNode) Global.previousTreePath.getLastPathComponent();
NodePro npt = (NodePro) current.getUserObject();
npt.setNodeancestors("activated");
}
}catch(Exception zxcv){
//save this path to edit later
TreePath path = Tree.getInstance().getPathForLocation(e.getX(), e.getY());
if (path != null){
Global.previousTreePath = path;
DefaultMutableTreeNode current = (DefaultMutableTreeNode) Global.previousTreePath.getLastPathComponent();
NodePro npt = (NodePro) current.getUserObject();
npt.setNodeancestors("activated");
}
}
}
}
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖 SSCCE。还有..你的问题是什么?