有才华的人,别忘记给滋润你的那块土壤施肥

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  28 随笔 :: 5 文章 :: 147 评论 :: 0 Trackbacks
      在很多软件中每个文本组件都有自定义的菜单,这个blogjava的编辑器就有这样的菜单如:Cut , Copy,Paste,Delete,Select All,在Swing中若也想在JTextField,JTextArea,JEditorPane,JTextPane等等这些组件中都提供如此自定义菜单的功能,每个都写继承类?或者加鼠标监听事件?但不管怎样弄都会实现效果,只不过这样动静很大,不好维护,今天在网上看到一个很是方便的方法。

       大家都知道,Swing中所有的事件都是进入java.awt.EventQueue这个队列中等待,然后通过dispatchEvent方法进行派发,那么我们在这里就写个继承EventQueue这个类,拦截所有的事件并对其进行处理,我们的文本组件都是继承与JTextComponent的,那么到这里我们就能为所有的文本组件定制一致的菜单了。效果如:
     



见代码:
package org.kissjava.swingx.core;

import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.JPopupMenu;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;

public class KJEventQueue extends EventQueue {
    @Override
     
protected void dispatchEvent(AWTEvent event)
            
super.dispatchEvent(event); 
     
            
// interested only in mouseevents 
            if(!(event instanceof MouseEvent)) 
                
return
     
            MouseEvent me 
= (MouseEvent)event; 
     
            
// interested only in popuptriggers 
            if(!me.isPopupTrigger()) 
                
return
     
            
// me.getComponent() retunrs the heavy weight component on which event occured 
            Component comp = SwingUtilities.getDeepestComponentAt(me.getComponent(), me.getX(), me.getY()); 
     
            
// interested only in textcomponents 
            if(!(comp instanceof JTextComponent)) 
                
return
     
            
// no popup shown by user code 
            if(MenuSelectionManager.defaultManager().getSelectedPath().length>0
                
return
     
            
// create popup menu and show 
            JTextComponent tc = (JTextComponent)comp; 
            JPopupMenu menu 
= new JPopupMenu(); 
            menu.add(
new CutAction(tc)); 
            menu.add(
new CopyAction(tc)); 
            menu.add(
new PasteAction(tc)); 
            menu.add(
new DeleteAction(tc)); 
            menu.addSeparator(); 
            menu.add(
new SelectAllAction(tc)); 
            
            Point pt 
= SwingUtilities.convertPoint(me.getComponent(), me.getPoint(), tc);
            menu.show(tc, pt.x, pt.y);
        }
 
     
class CutAction extends AbstractAction
            JTextComponent comp; 
         
            
public CutAction(JTextComponent comp)
                
super("Cut"); 
                
this.comp = comp; 
            }
 
         
            
public void actionPerformed(ActionEvent e)
                comp.cut(); 
            }
 
         
            
public boolean isEnabled()
                
return comp.isEditable() 
                        
&& comp.isEnabled() 
                        
&& comp.getSelectedText()!=null
            }
 
        }
 
         
        
// @author Santhosh Kumar T - santhosh@in.fiorano.com 
        class PasteAction extends AbstractAction
            JTextComponent comp; 
         
            
public PasteAction(JTextComponent comp)
                
super("Paste"); 
                
this.comp = comp; 
            }
 
         
            
public void actionPerformed(ActionEvent e)
                comp.paste(); 
            }
 
         
            
public boolean isEnabled()
                
if (comp.isEditable() && comp.isEnabled())
                    Transferable contents 
= Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this); 
                    
return contents.isDataFlavorSupported(DataFlavor.stringFlavor); 
                }
else 
                    
return false
            }
 
        }
 
         
        
// @author Santhosh Kumar T - santhosh@in.fiorano.com 
        class DeleteAction extends AbstractAction
            JTextComponent comp; 
         
            
public DeleteAction(JTextComponent comp)
                
super("Delete"); 
                
this.comp = comp; 
            }
 
         
            
public void actionPerformed(ActionEvent e)
                comp.replaceSelection(
null); 
            }
 
         
            
public boolean isEnabled()
                
return comp.isEditable() 
                        
&& comp.isEnabled() 
                        
&& comp.getSelectedText()!=null
            }
 
        }
 
         
        
// @author Santhosh Kumar T - santhosh@in.fiorano.com 
        class CopyAction extends AbstractAction
            JTextComponent comp; 
         
            
public CopyAction(JTextComponent comp)
                
super("Copy"); 
                
this.comp = comp; 
            }
 
         
            
public void actionPerformed(ActionEvent e)
                comp.copy(); 
            }
 
         
            
public boolean isEnabled()
                
return comp.isEnabled() 
                        
&& comp.getSelectedText()!=null
            }
 
        }
 
         
        
// @author Santhosh Kumar T - santhosh@in.fiorano.com 
        class SelectAllAction extends AbstractAction
            JTextComponent comp; 
         
            
public SelectAllAction(JTextComponent comp)
                
super("Select All"); 
                
this.comp = comp; 
            }
 
         
            
public void actionPerformed(ActionEvent e)
                comp.selectAll(); 
            }
 
         
            
public boolean isEnabled()
                
return comp.isEnabled() 
                        
&& comp.getText().length()>0
            }
 
        }
 
}


       到这里我们如何使用这个类呢?也很简单,只要在主程序的入口处加上下面这句就行了:
Toolkit.getDefaultToolkit().getSystemEventQueue().push(new KJEventQueue()); 

            也就是在main方法中调用,如:
 

public static void main(String[] args) {
        Toolkit.getDefaultToolkit().getSystemEventQueue().push(
new KJEventQueue()); 
        
// TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable(){
            
public void run(){
                
new KJEventQuequeDemo();
            }

        }
);
    }



资料来源:http://www.jroller.com/santhosh/entry/context_menu_for_textcomponents

posted on 2009-06-27 23:31 kissjava 阅读(1373) 评论(4)  编辑  收藏 所属分类: swing

评论

# re: Swing中为文本组件定制统一的菜单 2009-06-28 21:58 rochoc
不错,学习了,谢谢  回复  更多评论
  

# re: Swing中为文本组件定制统一的菜单 2009-06-28 23:50 zht
Santhosh 是个人才  回复  更多评论
  

# re: Swing中为文本组件定制统一的菜单 2009-06-29 00:03 枯宽
@zht
的确,最近没事都在看他以前的文章  回复  更多评论
  

# re: Swing中为文本组件定制统一的菜单 2009-07-01 13:26 找个美女做老婆
Java高手群:Java乐园,群号:28840096 Java乐园网站:http://www.javaly.cn 欢迎Java高手加入,大家一起交流经验,相互学习,共同进步  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: