java - JComboBox Selection Change -
everyone quite new java gui, having issue jcombobox , firing when removeallitems combo box refresh it, issue because getting selected items details , populating textboxes them firing @ point getting null pointer. there simple(ish) way have method on combobox called when selected item changed not when combo box contents changed?
code
combobox current method
private void customercomboactionperformed(java.awt.event.actionevent evt) { setdetails(); }
method setting items in combo box
public void setcustomers() { customercombo.removeallitems(); (customer curr : main.getnewcustomerlist().getcustomers()) { customercombo.additem(curr); } }
method setting details
public void setdetails() { customer selected = (customer) customercombo.getselecteditem(); forenametext.settext(selected.getforename()); surnametext.settext(selected.getsurname()); costperkgtext.settext(string.valueof(selected.getdeliverycost())); line1text.settext(string.valueof(selected.getcoladdress().getaddressline1())); line2text.settext(string.valueof(selected.getcoladdress().getaddressline2())); citytext.settext(string.valueof(selected.getcoladdress().getcity())); postcodetext.settext(string.valueof(selected.getcoladdress().getpostcode())); }
you not accounting case there no selection.
public void setdetails() { customer selected = (customer) customercombo.getselecteditem(); if (selected != null) { // there selection use } else { // example, clear text boxes } }
we expect changing contents of combo box might change selection shouldn't ignore it.
Comments
Post a Comment