Java Swing: Action Event to submit multiple variables -


i'm creating simple application let's me search employee's information far can search first name want add more variables search date of birth, address, profession etc submitted single action event on jbutton.

how go doing this? i've looked using if else , switch statement don't have clue how go forwards.

 private void searchactionperformed(java.awt.event.actionevent evt) {                                             string val1 = textfirstname.gettext();              try {           string sql = "select * employees first_name = '"+val1+"'";           stmt = con.createstatement();           rs = stmt.executequery(sql);           datatable.setmodel(dbutils.resultsettotablemodel(rs));         }       catch ( sqlexception err ) {             joptionpane.showmessagedialog(appointments.this, err.getmessage());     }  } 

what create dynamic panel grow users add more search options. essentially, have add search term button add new combo box , text field each time pressed (provided that, instance, last combo box in list not empty).

you need create class of sort:

public class searchterm {     private int id;     private string column;     private string value;      //getters , setters , rest.     ... } 

once create combo box (which contain fields users can search through) , text box, contain text, add new searchterm object list of search terms. id field come in handy should wish allow users remove and/or change existing search terms.

the next step create separate layer searching. make ui layer contain ui functionality. single responsibility pattern dictates class 1 thing. currently, class responsible ui , db operations.

this new layer have method takes in list of searchterm items. there construct search query iterating on searchterm objects provided , corresponding values. once done, return list of user objects resemble data in database. need execute on separate thread. need background workers this.

some additional points:

  1. please make sure use prepared statements instead of sql concatenation. is, application prone sql injection.

  2. at moment, performing search operation on event dispatcher thread (edt). not recommended since thread responsible maintaining ui. thus, if intensive operations on thread, ui suffer (by becoming less responsive or plain freezing). launching search operation on separate thread ensure edt not bogged down non ui related operations. update ui background thread through swingutilities.invokelater mechanism.


Comments