Customizing the Unified menu for text components in swing

Source: Internet
Author: User
Tags inheritance

In many software, each text component has a custom menu, and this Blogjava editor has this menu such as Cut, copy,paste,delete,select all, if you want to Jtextfield,jtextarea in swing, Jeditorpane,jtextpane, and so on. These components provide functionality for such a custom menu, each writing an inheritance class? Or a mouse monitor event? But in any case will achieve the effect, but this movement is very big, bad maintenance, today on the Internet to see a very convenient method.

As we all know, All of the events in swing are going into the java.awt.EventQueue queue and then distributed through the Dispatchevent method, so here we write an inheritance EventQueue class that intercepts and processes all of the events, and our text components are inherited and JT Extcomponent, so here we can customize the menu for all the text components. Effects such as:

See Code:

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 in mouseevents
if (!) ( Event instanceof MouseEvent))
Return

MouseEvent me = (mouseevent) event;

Interested 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 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;
}
}
}

How do we use this class here? Also very simple, as long as the main program at the entrance to add the following sentence on the line:

Toolkit.getdefaulttoolkit (). Getsystemeventqueue (). push (new Kjeventqueue ());

Which is called in the Main method, such as:

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();
             }
         });
     }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.