Swing嶄葎猟云怏周協崙由匯議暇汽
扮寂:2011-01-16 BlogJava kissjava
壓載謹罷周嶄耽倖猟云怏周脅嗤徭協吶議暇汽,宸倖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.g
|