Results 1 to 1 of 1
- 01-07-2011, 02:48 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
Get value of JComboBox text field
How can I get the value typed in a JComboBox where the typed String is NOT an existing JComboBox item?
I tried the following code:
but I guess I'm asking for the 'selected' value using 'getSelectedItem()' whereas I need to ascertain the String typed by the user in the JComboBox's text field. As a variation, I tried:Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class template { JComboBox comboBox; JFrame frame; template() { String items[] = {"Item1", "Item2", "Item3"}; comboBox = new JComboBox(items); comboBox.setEditable(true); frame = new Frame("Template"); comboBox.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent keyEvent) { System.out.println("A key was pressed"); String comboboxText = (String)comboBox.getSelectedItem(); System.out.println("The string " + comboboxText + "was entered."); } }); frame.add(comboBox); frame.setSize(200, 200); frame.setVisible(true); } public static void main(String args[]) { template t = new template(); } }
Java Code:String comboboxText = (String)comboBox.getEditor().getItem();
but it didn't work. Any way I can get the text typed in the JComboBox's text field? Thanks a lot.
Similar Threads
-
how unhighlight text in jcombobox?
By pinks_70986 in forum New To JavaReplies: 11Last Post: 06-01-2012, 01:13 PM -
Text-field templates.
By jdipierro in forum New To JavaReplies: 4Last Post: 05-14-2010, 12:48 AM -
Refreshing Text Field Value
By nehakuls in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 11-13-2009, 11:05 AM -
get numeric value from a text field
By Lehane_9 in forum New To JavaReplies: 2Last Post: 06-14-2008, 03:19 AM -
Regarding Text Field
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-05-2008, 11:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks