java - My program has a Country and State combobox, and from the state combobox it only gets the first value no matter what you choose -
so created program assignment have, creates sequential file data students. made country , state combobox, depending on country comes corresponding states, in sequential file (which .dat file) no matter state choose, in sequential file, state first one.for example if choose australia , tasmania state , state in sequential file new south wales. here code in advance
public assignment1st() { super("create student file"); try{ output=new dataoutputstream(new fileoutputstream("studentrec.dat")); } catch ( ioexception e ) { system.err.println( "file won't open properly/n" + e.tostring( ) ); system.exit( 1 ); } initialize(); //*******here starts country/state combobox build************************************** string[] countries = {"-choose","australia","belgium","brazil","canada","georgia","greece", "india","lithuania","macedonia"}; combobox_1 = new jcombobox<object>(countries); combobox_1.addactionlistener(this); combobox_1.setbounds(278, 142, 92, 20); frame.getcontentpane().add(combobox_1); // create sub combo box multiple models //state combobox combobox_2 = new jcombobox<string>(); combobox_2.additem("-choose-"); combobox_2.setbounds(452, 142, 109, 20); frame.getcontentpane().add(combobox_2); combobox_2.setprototypedisplayvalue("xxxxxxxxxx"); string[] australia = { "new south wales", "tasmania", "queensland" ,"victoria"}; states.put(countries[1], australia); string[] belgium = { "louxembourg", "hainaut", "flemish" }; states.put(countries[2], belgium); string[] brazil = { "amazonas", "mato grosso" }; states.put(countries[3], brazil); string[] canada = { "vancouver", "quebec" }; states.put(countries[4], canada); string[] georgia = {"tbilisi", "s.ossetia" }; states.put(countries[5], georgia); string[] greece = { "pelloponisos", "chalchidikis", "thesprotias" }; states.put(countries[6], greece); string[] india = { "jalpur", "kolkata", "new delhi" }; states.put(countries[7], india); string[] lithuania = { "akmene", "kretinga", "varena" }; states.put(countries[8],lithuania); string[] macedonia = { "bitola", "struga", "veles" }; states.put(countries[9], macedonia); }
code imports data
if ( studentid > 0 ) { //place comboboxez string sex=(string) combobox.getselecteditem(); output.writeutf(sex); string country=(string) combobox_1.getselecteditem(); output.writeutf(country); string state=(string) combobox_2.getselecteditem(); output.writeutf(state); string month=(string) combobox_3.getselecteditem(); output.writeutf(month); string day=(string) combobox_4.getselecteditem(); output.writeutf(day); string year=(string) combobox_5.getselecteditem(); output.writeutf(year); output.writeint(maths); output.writeint(buisness); output.writeint(programming); output.writeint(accounting); output.writeint(art); output.writeint(music);
and actionperformed country state comboboxes
public void actionperformed( actionevent e ) { //*************for state , country comboboxez********************* string country = (string)combobox_1.getselecteditem(); object o = states.get( country ); if (o == null) { combobox_2.setmodel( new defaultcomboboxmodel<string>() ); } else { combobox_2.setmodel( new defaultcomboboxmodel<string>( (string[])o ) ); } //**********done state , country comboboxez**********
this because part of code :
if (o == null) { comboboxstate.setmodel( new defaultcomboboxmodel<string>() ); } else { comboboxstate.setmodel( new defaultcomboboxmodel<string>( (string[])o ) ); }
is before this:
if (e.getsource()==btnenter){ addrecord( ) ; }
as such, combo box gets rearranged first, , record added.
if put second part before first one, works perfectly. that's it.
but, suggest check component caused action, , want do, else actions on components can cause operations expected other component perform. if want that, can experiment e.getactioncommand()
Comments
Post a Comment