java - Problems using JMenu with different choices -
i´ve been working on gui , i´ve run problems jmenu. gui in separate class takes actions of program rest of classes. problem using menu want 2 things: first, show correct panels based upon user choice , second, wait user input complete chosen task.
i´ve arranged 13 different if.. else clauses depending on user choice, part works correctly nescessary input options (panels) shown user need input data.
part 2 when user sees right panels , wants input information (int , strings) things don´t go intended. instead of waiting user input , take actions based on data program rushes forward , continues. since no data entered, needless say, output not intended one.
i´ll provide part of class it´s quite large.
class employee implements actionlistener { public void actionperformed(actionevent e) { if(e.getactioncommand().equals("1")) { panelb.setvisible(false); panelc.setvisible(false); paneld.setvisible(false); display.settext(bank.infobank()); } else if(e.getactioncommand().equals("2")) { panelb.setvisible(true); //show panels panelc.setvisible(true); paneld.setvisible(true); label2.settext("accountnumber: "); text2.settext(""); display.settext(""); } ... else if(e.getactioncommand().equals("5")) { panelb.setvisible(true); panelc.setvisible(false); paneld.setvisible(true); display.settext(""); if(e.getactioncommand().equals("ok") && !pnrtext.gettext().isempty()) //this jbutton when pressed should send data method { if(checkinput(pnrtext) == true) //method validate input { pnr = long.parselong(pnrtext.gettext()); //pnr input field bank.addsavingsaccount(pnr); //this method class } else { joptionpane.showmessagedialog(null, "only digits permitted!"); } }
this last part (actioncommand equals 5) 1 of places program doesn´t wait user input before continues , receives no input @ process.
this part of atm-program built around different jmenus, in place jradiobutton used works intended can´t work using jmenu , don´t want use jradiobutton many choices(13).
any input appreciated! /johan
"this last part (actioncommand equals 5) 1 of places program doesn´t wait user input before continues , receives no input @ process."
else if(e.getactioncommand().equals("5")) { panelb.setvisible(true); panelc.setvisible(false); paneld.setvisible(true); display.settext(""); if(e.getactioncommand().equals("ok") {
i don't know why expect behavior. not console program scanner waits input. event driven programming doesn't work that. 1 event get's 1 response. there's no waiting. in actionperformed
happens once. means when press 5
, corresponding if
perform. ok
-if
never performed because no event ever reach because it's trapped in 5-if
. quickes fix, give ok-block
it's own else-if
on same level 5-if
like said in comment. avoid these if statement. add anonymous listener each menu item.
okitem.addactionlistener(new actionlistener(){ public void actionperforemd(actionevent e) { } });
but can excel beyond , use action
. prefer go route menu items, number of reasons.
also aside, should using cardlayout
switch between panels.
Comments
Post a Comment