Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. JComboBox and ActionListener.

JComboBox and ActionListener.

Scheduled Pinned Locked Moved Java
databasequestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chdboy
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups