swing - Change button text after click, then revert the changes after clicking again using JAVA ActionEvent -
i displaying code wrote changing text when button clicked. trying default text clicking button again, unable so. please advice.
import javax.swing.*; import java.awt.event.*; public class actions extends jframe implements actionlistener{ jpanel pnl=new jpanel(); imageicon ic1=new imageicon("/home/mudit/downloads/duke.png"); jbutton btn1=new jbutton("click",ic1); jtextarea ta=new jtextarea(5,37); public static void main(string[]args) { actions gui=new actions(); } public actions() { super("swing window"); setsize(500,300); setdefaultcloseoperation(exit_on_close); add(pnl); btn1.sethorizontaltextposition(jbutton.center); btn1.setverticaltextposition(jbutton.bottom); ta.setlinewrap(true); ta.setwrapstyleword(true); pnl.add(btn1); pnl.add(ta); btn1.setenabled(true); ta.settext("button enabled"); btn1.addactionlistener(this); setvisible(true); } public void actionperformed(actionevent event) { if(event.getsource()==btn1) { ta.settext("button clicked"); } } }
you can check text of button if
-statement:
if (event.getsource() == btn1) { if (ta.gettext().equals("button enabled")) { ta.settext("button clicked"); } else { ta.settext("button enabled"); } }
Comments
Post a Comment