swing - Java - ItemListener not listening to my components? -
i took segment of program , put code here. basically issue item listener method isnt being called. here if user selects right answer , presses "submit" actionlistener method tell him/her if got answer right.
static boolean answer = false; // returns true if user got question right public void quizpanel() { quizpanel = new jpanel(); quizpanel.setlayout(null); // default layout jlabel label = new jlabel( "true/false- java object orientated?"); label.setbounds(10, 50, 400, 20); quizpanel.add(label); option1 = new jcheckbox("true"); option1.setselected(false); option1.setbounds(10, 70, 120, 40); quizpanel.add(option1); option1.additemlistener(this); // adds item listener option2 = new jcheckbox("false"); option2.setselected(false); option2.setbounds(40, 70, 120, 40); quizpanel.add(option2); option2.additemlistener(this); /////////////////////////////////////// jbutton submit = new jbutton("submit answer:"); submit.setbounds(10, 100, 150, 20); quizpanel.add(submit); submit.addactionlistener(new actionlistener() { // action listener public void actionperformed(actionevent evt2) { system.out.println ("your answer " + answer); // got right? } }); } public void itemstatechanged1(itemevent e1) { object source = e.getitemselectable(); if (source == option1) { answer = true; } else { //nothing- stays false } }
nothing happens , doesn't tell user if he/she got right/wrong.
quizpanel = new jpanel();
use standard java naming conventions. variable names should not start upper case character:
quizpanel.setlayout(null); ... label.setbounds(10, 50, 400, 20);
don't use null layout , setbounds(). swing designed used layout manager.
public void itemstatechanged1(itemevent e1) throws ioexception
i don't know how code compiles. implementing wrong method. also, why itemlistener throw ioexception?
too many little mistakes guess doing wrong. post proper sscce.
Comments
Post a Comment