java - combo box that has district names as items and whose value are displayed in a textfield interms of the districts' code -
i want text field display codes of districts corresponding district name selected user combo box in same interface returning codes first , last districts. can do? codes , names in same database. sample code used below:
private void stationspopupmenuwillbecomeinvisible(javax.swing.event.popupmenuevent evt) { // todo add handling code here: string connectionurl = "jdbc:mysql://localhost:3306/crime_records"; string dbuser = "root"; string dbpwd = "cfm"; connection conn; resultset rs; try{ string tmp = (string)stations.getselecteditem(); string sql ="select * station_codes station_name = ?"; preparedstatement pst; class.forname("com.mysql.jdbc.driver"); conn= (connection) drivermanager.getconnection(connectionurl, dbuser,dbpwd); pst=(preparedstatement) conn.preparestatement(sql); pst.setstring(1, tmp); rs=pst.executequery(); if(rs.next()){ string n = rs.getstring("code"); code.settext(n); } } catch(exception e) { joptionpane.showmessagedialog(null,e); } }
it difficult find cause of problem , analyze why application returning codes first , last districts. hovercraft full of eels remarked, seems relevant part of code missing?
for user interface part of question, consider using code example below:
import java.awt.borderlayout; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jtextfield; import javax.swing.windowconstants; /** * https://stackoverflow.com/questions/29852371/ * combo-box-that-has-district-names-as-items-and-whose-value-are-displayed-in-a-te */ public class districtsgui { private static final district[] districts = { new district("name 1", "code 1"), new district("name 2", "code 2"), new district("name 3", "code 3"), new district("name 4", "code 4"), new district("name 5", "code 5"), new district("name 6", "code 6") }; public static void main(final string[] args) { new districtsgui().getstarted(); } private void getstarted() { final jframe frame = new jframe("stack overflow: districts"); frame.setbounds(100, 100, 800, 600); frame.setdefaultcloseoperation(windowconstants.exit_on_close); final jcombobox<district> districtcombobox = new jcombobox<>(districts); final jtextfield districttextfield = new jtextfield(""); frame.getcontentpane().add(districtcombobox, borderlayout.north); frame.getcontentpane().add(districttextfield, borderlayout.center); districtcombobox.addactionlistener(e -> { final district selecteddistrict = (district) districtcombobox.getselecteditem(); districttextfield.settext(selecteddistrict.getcode()); }); // select first item in combo box explicitly, // initialize text field too. districtcombobox.setselectedindex(0); frame.setvisible(true); } } the district class this:
public class district { private final string name; private final string code; public district(string name, string code) { this.name = name; this.code = code; } public string getname() { return name; } public string getcode() { return code; } @override public string tostring() { return name; } }
Comments
Post a Comment