Solved

How to have a global right click menu on all text components in my app?

Posted on 2006-06-23
17
310 Views
Last Modified: 2012-06-22
I need to put a right click menu on all text components in my app.

I want something that will work on the standard swing text components (So no 'extend component and refactor' answers please ) and also on any JDialogs that are created from my app.

Any ideas?
0
Comment
Question by:riaancornelius
  • 7
  • 5
  • 2
  • +3
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16968382
What's up with JPopupMenu? ;-)
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 16968412
It's to do a copy / paste menu for each text component throughout the app (which is at quarter million lines of code, so I don't want to mess with any existing code if at all possible.)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16968471
Wouldn't it be better to put copy/paste items in the *main* menu anyway?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:hoomanv
ID: 16968631
you cant do it without adding the functionality to TextComponent itself

look at the example here: PopupMenuDemo.java
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#popup

you have two options
 1 add mouselistener to each text field individualy
 2 implement your own text component and extend from it
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16968682
Maybe you like to solve this with a function key? This is a solution for F1:

Add the following code when creating the JFrame:

         Toolkit.getDefaultToolkit().addAWTEventListener(new Ui(), AWTEvent.KEY_EVENT_MASK);

Add the interface to your action class:

                                                  ... implements AWTEventListener

And implement:

   /** (non-Javadoc)
    * @see java.awt.event.AWTEventListener#eventDispatched(java.awt.AWTEvent)
    */
   public void eventDispatched(AWTEvent event)
   {
      if(event instanceof KeyEvent)
      {
         KeyEvent ke = (KeyEvent)event;

         if(ke.getID() == KeyEvent.KEY_PRESSED && ke.getKeyCode() == KeyEvent.VK_F1)  // Choose a function key.
         {
            popMyDialog();           // Here comes whatever action you want.
         }
      }
   }


;JOOP!
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 16973389
try the following EventQueue:

public class PopupEventQueue extends EventQueue
{
    protected void dispatchEvent(AWTEvent event)
    {
        super.dispatchEvent(event);
 
        if (event instanceof MouseEvent)
        {
           MouseEvent me = (MouseEvent)event;
           if (me.isPopupTrigger())
           {
               Component comp = SwingUtilities.getDeepestComponentAt(me.getComponent(),
                                                me.getX(), me.getY());
               if (comp instanceof JTextComponent)
               {
                   if (MenuSelectionManager.defaultManager().getSelectedPath().length==0)
                   {
                       // do popup

                      JTextComponent tc = (JTextComponent)comp;
                      JPopupMenu menu = new JPopupMenu();
                      menu.add(new CutAction(tc));
                      menu.add(new CopyAction(tc));
                      menu.add(new PasteAction(tc));
                      Point p = SwingUtilities.convertPoint(me.getComponent(), me.getPoint(), tc);
                      menu.show(tc, p.x, p.y);
                   }
                }
           }
       }
    }
}
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 100 total points
ID: 16973621
the complete example is here,

import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;
import javax.swing.*;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseEvent;
import java.awt.*;

public class TestGlobalRightClick extends JFrame{
    public static final String rightClickAction = "RightClickAction" ;

    private void buildComponents(){
        JPanel pnlMain = new JPanel( new GridBagLayout() ) ;
        JLabel lbl1 = new JLabel( "One" ) ;
        JTextField txt1 = new JTextField( 10 ) ;
        JLabel lbl2 = new JLabel( "Two" ) ;
        JTextField txt2 = new JTextField( 10 ) ;
        JButton btn = new JButton( "Click" ) ;

        pnlMain.add( lbl1, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 5, 5 ), 0, 0 ) ) ;
        pnlMain.add( txt1, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 5, 5 ), 0, 0 ) ) ;
        pnlMain.add( lbl2, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 5, 5 ), 0, 0 ) ) ;
        pnlMain.add( txt2, new GridBagConstraints( 1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5, 5, 5, 5 ), 0, 0 ) ) ;

        getContentPane().add( pnlMain, BorderLayout.CENTER ) ;
        getContentPane().add( btn, BorderLayout.SOUTH ) ;

        pack() ;
        setVisible( true ) ;
    }

    public static void main( String arg[] ){
        Toolkit.getDefaultToolkit().addAWTEventListener( new MyAWTListener(), AWTEvent.MOUSE_EVENT_MASK );
        TestGlobalRightClick tgrc = new TestGlobalRightClick() ;
        tgrc.buildComponents();
    }

    public static class MyAWTListener implements AWTEventListener {

        public void eventDispatched( AWTEvent awe ){
            MouseEvent me = ( MouseEvent )awe ;
            if( ! me.isPopupTrigger() ) return ;

            Object source = me.getSource() ;
            if( source instanceof JTextComponent ){
                JTextComponent text = ( JTextComponent )source ;
                JPopupMenu menu = new JPopupMenu();
                menu.add( new DefaultEditorKit.CutAction() );
                menu.add( new DefaultEditorKit.CopyAction() );
                menu.add( new DefaultEditorKit.PasteAction() );
                Point p = SwingUtilities.convertPoint( me.getComponent(), me.getPoint(), text );
                menu.show( text, p.x, p.y );
            }
        }
    }
}
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16973628
you may have to add the focus event mask for hiding the popup when the focus lost for the app.
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 16990230
Thanks guys. I'll have a look at your ideas as soon as I get a moment. I was away for the weekend and only got back yesterday, So I'm still a bit swamped.

I think objects solution of extending the eventqueue is most likely to be what I'm looking for.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16990259
>>I think objects solution of extending the eventqueue is most likely to be what I'm looking for.

just curious, what is the advantage over this?
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 16990337
>> just curious, what is the advantage over this?

If you're referring to this:
>> public class TestGlobalRightClick extends JFrame{
>>    public static final String rightClickAction = "RightClickAction" ;
>>    private void buildComponents(){
>> .....

At first glance I think that will only work for the contents of the JFrame, Not for any JDialogs etc...

If I'm wrong, please don't take my head of yet. Like I say, I've only glanced at the solutions.

I will try all the solutions before making a decision on which works better.

:)
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16994680
>>At first glance I think that will only work for the contents of the JFrame, Not for any JDialogs etc...

Nope, it is going to work for all JTextComponent in a swing application regardless of it's container! Ya, you can try all and choose a one that fits you :)

I just wished to know advantageous of other solutions, nothing else ;)
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 16998634
No problem. I'll still be busy today, but should have the chance to look at these solutions tonight. I will let you know why I choose whichever solution I do.

Objects: This might be a bit of a dumb question, but what do I do with that PopupEventQueue?
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 16998689
Solutions that won't work:

CEHJ: At the moment we hava copy/paste functionality in the main menu. Most users just doesn't look there.
hoomanv: My original question expressly stated that I don't want to mess with individual components. And it can be done :)
sciuriware: Although technically your solution is fine, we have tried it, and it's the same problem. Users just doesn't understand it. (And yes, we do have it very well documented in a pretty easy to use help function. Users apparently can't read either...)

So, I'll look at objects and ksivananth's solutions tonight. They seem like pretty much the same thing though?
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 16998715
Never mind objects, found how to push the eventqueue onto the systemEventQueue
0
 
LVL 9

Author Comment

by:riaancornelius
ID: 16998850
I've tested both solutions. Both works exactly the way I want, and it still seems like pretty much the same solution to me. So, because objects was first I'll give him most of the points, with a few for ksivananth's effort.

Any objections?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16998995
No objections!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
session migration servlets 2 42
jsp login check 12 42
Setup GlassFish 4 21
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question