java - boolean method canPaste() does not return false even when the condition is satisfied -
when string cpbtxt null, method should return false not return anything.please help. there no errors. i've tried "cpbtxt.equals()" still no luck. please tell me going wrong? here complete code:
import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.undo.*; import javax.swing.text.*; class sample extends jframe implements menulistener { jmenubar mb; jmenu em; jmenuitem paste; jtextarea t; public sample() { setlayout(new borderlayout()); setsize(400,400); setdefaultcloseoperation(exit_on_close); mb = new jmenubar(); em = new jmenu("edit"); paste = new jmenuitem("paste"); add(mb,borderlayout.north); mb.add(em); em.add(paste); em.addmenulistener(this); t = new jtextarea(); add(t,borderlayout.center); } public boolean canpaste() throws exception { clipboard cpb = toolkit.getdefaulttoolkit().getsystemclipboard(); string cpbtxt = (string)cpb.getdata(dataflavor.stringflavor); if(cpbtxt == null) return false; else return true; } public void menuselected(menuevent me) { if(me.getsource() == em) { try { paste.setenabled(canpaste()); } catch(exception e) { system.out.println(e); } } } public void menudeselected(menuevent me) { } public void menucanceled(menuevent me) { } } class samplemain { public static void main(string[]args) { sample s1 = new sample(); s1.setvisible(true); } }
change menuselected
method this:
public void menuselected(menuevent me) { if(me.getsource() == em) { boolean canpasteresult = false; try { canpasteresult = canpaste(); } catch(exception e) { system.out.println(e); } paste.setenabled(canpasteresult); } }
with code method didn't change enabled
state of menuitem if canpaste
method throwed exception example if have selected file in clipboard.
code situation result in disabled state.
Comments
Post a Comment