Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2005

Tip: Looking for answers? Try searching our database.

jtree, cell editors, and event processing

Thread view: 
AC - 07 Mar 2005 17:30 GMT
Greetings.

I have a custom JTree which uses custom cell editors and renderers
(JPanels). Everything works fine except my users are complaining because
they have to click twice to get the popup menu to show up. The first click
selects the cell and the second brings up the context menu. I have been
googling a lot trying to find a solution to no avail as yet.

I have tried a number of things:

- add a mouse listener to the tree an repost the mouse event if it has
popupTrigger set
- add a selection listener and try to get the cell to start editing after
selection

None have had the desired effect. Can anyone out there make a
recommendation? Christian?

Many Thanks,

Allan
AC - 23 Mar 2005 14:42 GMT
> Greetings.
>
[quoted text clipped - 13 lines]
> None have had the desired effect. Can anyone out there make a
> recommendation? Christian?

Bump. Nobody has an opinion on this?

Allan
Christian Kaufhold - 24 Mar 2005 16:29 GMT
>> I have a custom JTree which uses custom cell editors and renderers
>> (JPanels). Everything works fine except my users are complaining because
[quoted text clipped - 8 lines]
>> - add a selection listener and try to get the cell to start editing after
>> selection

Your problem specification is too vague. What is "the popup menu"? Is
it for the JTree or the editor component? What is your editor implementation
(code)?

Christian
AC - 24 Mar 2005 18:27 GMT
>>> I have a custom JTree which uses custom cell editors and renderers
>>> (JPanels). Everything works fine except my users are complaining because
[quoted text clipped - 15 lines]
> implementation
> (code)?

Whoop, thanks for jumping in Christian. (By the way, through the magic of
Google, you have helped me many, many times in the past, and you didn't even
know it! ;-)

The implementation is rather large. If this posting doesn't provide enough
detail, I'll try to carve out enough of a chunk of code to demo the problem.

The JTree has custom cell editors, where each editor is a different derived
kind of JPanel. These panels have sevarl subcomponents, usually JLabels with
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.

Allan
Christian Kaufhold - 28 Mar 2005 22:33 GMT
> "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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.