java - How to cut/copy/paste text to JTextField when some JComponent is clicked? -
i'm creating custom popup menu, using extended jcomponent
menu items , extended jwindow
hold them. question - how send signal jcomponent
instance when it's clicked (has mouselistener
) jtextfield
perform cut/copy/paste actions?
edit:
i try explain more precisely.
jtextfield class (simplified):
public class textinputfield extends jtextfield implements focuslistener { private menupopupwindow popup; public textinputfield() { popup = new menupopupwindow();//menupopupwindow class extends jwindow menuitem paste = new menuitem("paste", new imageicon(getclass().getresource("/images/paste_icon.png")), "ctrl+v");//menuitem class extends jcomponent, has implemented mouselistener - , when mouseclicked(mouseevent e) occurs, somehow action signal have sent class menuitem copy = .... menuitem cut = .... action pasteaction = getactionmap().get(defaulteditorkit.pasteaction); paste.setaction(pasteaction);//how make work? popup.addmenuitem(paste); popup.addmenuitem(cut); popup.addmenuitem(copy); } }
how right?
in light of posted code, think need in textinputfield class, add:
paste.addactionlistener(pasteaction);
then in menuitem class have put in code call action listeners.
public class menuitem implements mouselistener { ... @override public void mouseclicked(mouseevent event) { actionlistener[] listeners = (actionlistener[]) menuitem.this.getlisteners(actionlistener.class); for(int = 0; < listeners.length; i++) { listeners[i].actionperformed ( new actionevent(menuitem.this,someid, somecmdname) ); } }
in class extends jcomponent (i'll call class 'a') need reference jtextfield. simple way add private instance variable of type jtextfield class a, , pass in jtextfield through constructor.
so class should this:
public class extends jcomponent implements actionlistener { private jtextfield updatefield; public a(jtextfield updatefield[,<your other contructor arguments>...]) { this.updatefield = updatefield; this.addactionlistener(this); } public void actionperformed(actionevent event) { if(event.getsource().equals(this) { //copy, paste or whatever jtextfield //by way of this.updatefield; //e.g. this.updatefield.settext(...); //or pass event along jtextfield's handlers //this.updatefield.dispatchevent(event); } } }
then have remember pass jtextfield constructor when create component
Comments
Post a Comment