Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Jcombobox get text when user type something

  1. #1
    sh4rif is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Jcombobox get text when user type something

    Hello every one

    I have a JComboBox and i want to get the user typed string to search through database

    currently i am useing key released event with the following code:

    Java Code:
    public void keyReleased(KeyEvent e) {
    		char c = e.getKeyChar();
    		//System.out.println("Key Released" + c);
    		String s =  Character.toString(c);
    		System.out.println(s);
    		//cmbProductName.getEditor().getEditorComponent().getT
    		//searchProduct()
    		
    	}
    by using e.getKeyChar() i only get one character what i want the whole string with all the character to search in the database... thanks

    sorry for my poor english once again folks...

  2. #2
    sh4rif is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Jcombobox get text when user type something

    this is full code of the class
    Java Code:
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class Test extends JFrame implements KeyListener{
    		
    	private static final long serialVersionUID = 1L;
    	
    	private Connection con = null;
    	private PreparedStatement pstmt = null;
    	private ResultSet rs = null;
    	private List<Character> list = new ArrayList<Character>();
    	private JPanel panel;
    	JComboBox<Object> cmbProductName;
    	
    	public Test() {
    		super("Test Combo box");
    		list = new ArrayList<Character>();
    		
    		setSize(300, 200);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		panel = new JPanel(null);
    		cmbProductName = new JComboBox<Object>();
    		//searchProduct(pName)
    		
    		cmbProductName.setBounds(10, 20, 150, 20);
    		cmbProductName.setEditable(true);
    		cmbProductName.getEditor().getEditorComponent().addKeyListener(this);
    		//cmbProductName.addKeyListener(this);
    		panel.add(cmbProductName);
    		add(panel);
    		setVisible(true);
    	}
    	
    	
    	
    	public void searchProduct(String pName){
    		String sql = "SELECT `product_id` , `product_name` , `product_type` " +
    					 "FROM `supplier_products` " +
    					 "WHERE `product_name` LIKE ?";
    		con = DeliConnection.openConnection();
    		try{
    			pstmt = con.prepareStatement(sql);
    			pstmt.setString(1, pName);
    			rs = pstmt.executeQuery();
    			while(rs.next()){
    				System.out.println(rs.getString(2));
    			}
    			rs.close();
    			pstmt.close();
    		}catch(SQLException e){
    			JOptionPane.showMessageDialog(null, "some errors");
    			e.printStackTrace();
    		}
    		DeliConnection.closeConnection(con);
    	}
    	
    	public static void main(String[] args){
    		Test t = new Test();
    		t.searchProduct("%t%");
    		//System.out.println("main");
    	}
    
    
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		//System.out.println("Key Typed");
    		
    	}
    
    
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		//System.out.println("Key Pressed");
    		
    	}
    
    
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		
    		list.add(e.getKeyChar());
    		StringBuilder sb  = new StringBuilder();
    		sb.append(list.get(0));
    		//System.out.println("Key Released" + c);
    		//String s =  Character.toString();
    		System.out.println(sb);
    		//cmbProductName.getEditor().getEditorComponent().getT
    		//searchProduct()
    		
    	}
    
    
    
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Jcombobox get text when user type something

    currently i am useing key released event...
    That doesn't sound right to me - too complicated!

    Have a look at the discussion of combo boxes in Oracle's Tutorial. The second example they give involves an editable combobox and, in the second example, they use an action listener (and getSelectedItem()) to read what the user entered.

    Your English is fine! But ask if there's anything in the Tutorial that's difficult to understand.
    sh4rif likes this.

Similar Threads

  1. Replies: 8
    Last Post: 06-30-2011, 03:08 PM
  2. Get value of JComboBox text field
    By nik_meback in forum AWT / Swing
    Replies: 0
    Last Post: 01-07-2011, 03:48 PM
  3. Need help with highlighting text in a jcombobox
    By cjmartin in forum AWT / Swing
    Replies: 3
    Last Post: 08-09-2010, 07:21 PM
  4. Adding JComboBox type column to the JTable ??
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 04-10-2010, 04:03 PM
  5. Replies: 0
    Last Post: 05-26-2008, 05:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •