> "Christian Kaufhold" <usenet@chka.de> wrote in message
>>
[quoted text clipped - 25 lines]
> either text or an icon. The JPanel itself has a popup menu as do the
> subcomponents. The JTree itself does not have a popup menu.
I think this is really a bug in BasicTreeUI: unless the editor activa-
tion happens with the left mouse button pressed, it will immediately
stop forwarding the mouse events to the new editor because then it
removes itself (see BasicTreeUI.MouseInputHandler.mouseExited) as soon
as a mouse-exited event arrives.
What follows is a hack to avoid this. At least this should be made only
to become active in more specific situations.
import javax.swing.*;
import java.awt.event.*;
import javax.swing.tree.*;
import java.awt.Component;
import javax.swing.event.*;
import java.util.EventObject;
public class TreeEditorPopup
{
public TreeEditorPopup()
{
}
public static void main(String[] args)
{
JTree t = new JTree()
{
protected void processMouseEvent(MouseEvent e)
{
if (e.getID() == MouseEvent.MOUSE_EXITED && SwingUtilities.isRightMouseButton(e))
super.processMouseEvent(new MouseEvent
(e.getComponent(), e.getID(), e.getWhen(), e.getModifiersEx() | InputEvent.BUTTON1_DOWN_MASK, e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
else
super.processMouseEvent(e);
}
};
t.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
System.err.println("TREE: "+e);
}
public void mouseReleased(MouseEvent e)
{
System.err.println("TREE: "+e);
}
});
t.setEditable(true);
t.setCellEditor(new DefaultTreeCellEditor2(t, (DefaultTreeCellRenderer)t.getCellRenderer(), new ProblemEditor()));
JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(t));
f.setSize(500, 400);
f.setVisible(true);
}
private static class DefaultTreeCellEditor2
extends DefaultTreeCellEditor
{
public DefaultTreeCellEditor2
(JTree t, DefaultTreeCellRenderer r, TreeCellEditor e)
{
super(t, r, e);
}
public boolean isCellEditable(EventObject e)
{
return true;
}
}
private static class ProblemEditor
extends AbstractCellEditor
implements TreeCellEditor
{
private JTextField textField = new JTextField();
private JPopupMenu menu;
{
menu = new JPopupMenu();
menu.add(new JMenuItem("Test"));
menu.add(new JMenuItem("More"));
textField.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
System.err.println(e);
popup(e);
}
public void mouseReleased(MouseEvent e)
{
System.err.println(e);
popup(e);
}
private void popup(MouseEvent e)
{
if (menu.isPopupTrigger(e))
menu.show(e.getComponent(), e.getX(), e.getY());
}
});
}
public Object getCellEditorValue()
{
return textField.getText();
}
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean selected, boolean expanded,
boolean leaf, int row)
{
textField.setText(value.toString());
return textField;
}
}
}
AC - 31 Mar 2005 16:21 GMT
>> "Christian Kaufhold" <usenet@chka.de> wrote in message
[snip]
I tried your sample, Christian, and it works stand alone. I expect to get
back to this problem in a couple of days, and I'll try your suggested
changes in my application and report back here.
A big thanks for the help.
Allan
AC - 20 Apr 2005 13:42 GMT
I got my code working. It turned out that one of our developers had
customized the BasicUI for the tree, so I had to make a more complex fix
than your example showed. But between your first and second responses, I had
enough pieces to get this done.
Many thanks, Christian!
Allan