JComboBox and ActionListener.
Java
1
Posts
1
Posters
0
Views
1
Watching
-
What I want to do is,when ever I click on any Item selected in the Combo Box it should display the result accordingly. My code does that ,but I wanted to know is there any way that I don't use lots of IF statements?Right now it is according to Index of combo box items.
else if(e.getSource().equals(jcb1))
{
int selectedindex = jcb1.getSelectedIndex();
String comb_string = (String)jcb1.getSelectedItem();
if(selectedindex==1)
{
try {//String sql\_command ="select \* , count(\*) over (partition by 1) total\_rows from Employer where Employername = ? "; String sql\_command ="select \* from Employer where Employername = ? "; PreparedStatement st=con.prepareStatement(sql\_command); st.setString(1,comb\_string); Result = st.executeQuery(); int test =0; String Store=""; String Response =""; test++; if(Result.next()) { String add1=Result.getString(3); first.setText(add1); String add2=Result.getString(28); second.setText(add2); String add3=Result.getString(4); third.setText(add3); String add4=Result.getString(6); fourth.setText(add4); if(test !=0) { /\*Result.absolute(test); DisplayData();\*/ Response = "Number of Records that Matches :"+test+Store; JOptionPane.showMessageDialog(null, Response); } else { JOptionPane.showMessageDialog(null,"Could not be found"); } } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
EDIT: I think
if(jcb1.getSelectedItem().equals(comb_string))
this is the solution. I have a JTextField instead ,but what this code does is ,it adds the name which I search into JComboBox and remembers it till the JFrame is open.