Results 1 to 3 of 3
- 06-07-2012, 01:26 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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:
by using e.getKeyChar() i only get one character what i want the whole string with all the character to search in the database... thanksJava 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() }
sorry for my poor english once again folks...
- 06-07-2012, 01:31 AM #2
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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() } }
- 06-07-2012, 01:39 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Jcombobox get text when user type something
That doesn't sound right to me - too complicated!currently i am useing key released event...
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.
Similar Threads
-
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By viks in forum EclipseReplies: 8Last Post: 06-30-2011, 02:08 PM -
Get value of JComboBox text field
By nik_meback in forum AWT / SwingReplies: 0Last Post: 01-07-2011, 02:48 PM -
Need help with highlighting text in a jcombobox
By cjmartin in forum AWT / SwingReplies: 3Last Post: 08-09-2010, 06:21 PM -
Adding JComboBox type column to the JTable ??
By Stephen Douglas in forum New To JavaReplies: 2Last Post: 04-10-2010, 03:03 PM -
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By luislopezco in forum Advanced JavaReplies: 0Last Post: 05-26-2008, 04:26 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks