Posted 16 Jul Link to this post. Posted 19 Jul Link to this post. Posted 09 Sep Link to this post. Posted 10 Sep Link to this post. Posted 11 Aug Link to this post. Posted 22 May in reply to Shinu Link to this post. Posted 07 Jun Link to this post. Any suggestions?? Posted 16 Feb in reply to Shinu Link to this post. Posted 16 Feb in reply to Jeremy Link to this post. Posted 19 Feb Link to this post. Posted 20 Sep Link to this post. All Products.
SelectedItem. Member since: Mar Member since: Sep Nencho Admin.Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. A JComboBoxwhich lets the user choose one of several choices, can have two very different forms. The default form is the uneditable combo box, which features a button and a drop-down list of values. The second form, called the editable combo box, features a text field with a small button abutting it.
The user can type a value in the text field or click the button to display a drop-down list. Here's what the two forms of combo boxes look like in the Java look and feel:. Combo boxes require little screen space, and their editable text field form is useful for letting the user quickly choose a value without limiting the user to the displayed values.
Other components that can display one-of-many choices are groups of radio buttons and lists. Groups of radio buttons are generally the easiest for users to understand, but combo boxes can be more appropriate when space is limited or more than a few choices are available. Lists are not terribly attractive, but they're more appropriate than combo boxes when the number of items is large say, over 20 or when selecting multiple items might be valid.
Because editable and uneditable combo boxes are so different, this section treats them separately. This section covers these topics:.
How do I set and get the selected item in JComboBox?
The following code, taken from ComboBoxDemo. This combo box contains an array of strings, but you could just as easily use icons instead. To put anything else into a combo box or to customize how the items in a combo box look, you need to write a custom renderer.
An editable combo box would also need a custom editor. Refer to Providing a Custom Renderer for information and an example. The preceding code registers an action listener on the combo box. To see the action listener implementation and learn about other types of listeners supported by combo box, refer to Handling Events on a Combo Box.
No matter which constructor you use, a combo box uses a combo box model to contain and manage the items in its menu. When you initialize a combo box with an array or a vector, the combo box creates a default model object for you.
Be careful when implementing a custom model for a combo box. Refer to the API tables to see which methods are affected. One way to get the list data event firing code for free is to make your combo box model a subclass of AbstractListModel. Here's the code from ComboBoxDemo. This action listener gets the newly selected item from the combo box, uses it to compute the name of an image file, and updates a label to display the image.
The combo box fires an action event when the user selects an item from the combo box's menu. See How to Write an Action Listenerfor general information about implementing action listeners.
Combo boxes also generate item events, which are fired when any of the items' selection state changes. Only one item at a time can be selected in a combo box, so when the user makes a new selection the previously selected item becomes unselected. Thus two item events are fired each time the user selects a different item from the menu. If the user chooses the same item, no item events are fired.
Use addItemListener to register an item listener on a combo box. How to Write an Item Listener gives general information about implementing item listeners.Warning: Swing is not thread safe. For more information see Swing's Threading Policy. Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.Java Swing JCombobox Tutorial getSelectedItem, getSelectedIndex ,setEditable
Please see XMLEncoder. JComponent JComponent.
Container Container. Component Component. BaselineResizeBehaviorComponent. BltBufferStrategyComponent. String getActionCommand Returns the action command that is included in the event sent to action listeners.
E getPrototypeDisplayValue Returns the "prototypical display" value - an Object used for the calculation of the display height and width. Object getSelectedItem Returns the current selected item. Object  getSelectedObjects Returns an array containing the selected item. Do not access directly or override.
Use the accessor methods instead. See Also: getModelsetModel javax. See Also: getRenderersetRenderer javax. See Also: getEditorsetEditor javax. See Also: setKeySelectionManager javax. See Also: setActionCommand java. Since the ComboBoxModel is provided, a combo box created using this constructor does not create a default combo box model and may impact how the insert, remove and add methods behave.
By default the first item in the array and therefore the data model becomes selected. By default the first item in the vector and therefore the data model becomes selected. The default data model is an empty list of objects. Use addItem to add items. By default the first item in the data model becomes selected.
JComboBox: getSelectedItem() : JComboBox « javax.swing « Java by API
Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I want to set the selected index in a JComboBox by the value not the index.
How to do that? Ok, I have modified my question a bit. I forgot that i have a custom item inside my JComboBox that makes it a bit more difficult. So still, how do i get this done? You could have found it yourself by just reading the javadoc.
Why not take a collection, likely a Map such as a HashMap, and use it as the nucleus of your own combo box model class that implements the ComboBoxModel interface? Then you could access your combo box's items easily via their key Strings rather than ints. There are some caveats or potentially unexpected behavior as explained in the javadoc. Make sure to read that.
The right way to set an item selected when the combobox is populated by some class' constructor as milosz posted :. Just call comboBox.
I have set an integer value as id for the combobox. I tried combobox. Method Object JComboBox. If the string is empty, comboBox. So better to typecast by String. Learn more. Get combobox value in Java swing Ask Question. Asked 7 years, 8 months ago. Active 2 years, 3 months ago. Viewed k times. I need to get the integer value of the combobox in Swing. Your update is incomplete. What is the type of projectIdfor example? Active Oldest Votes.
Pavel Bucek 5, 23 23 silver badges 42 42 bronze badges. I am getting only a string value when i use the above. Romain Guidoux 3, 3 3 gold badges 23 23 silver badges 44 44 bronze badges.
Sandeep Sandeep 61 1 1 silver badge 1 1 bronze badge. Sign up or log in Sign up using Google. Sign up using Facebook.Knute Snortum wrote: My understanding is that one usually backs Table data with objects in a row, not columns. So for instance, you get a selected row from the Table and it returns a Person. So your controller compiles? I don't see where firstName, lastName, and address are defined or injected. And like I said, work from the Person object, not the individual columns.
Does make sense? I can explain in more detail if necessary. Anel Omic wrote: I have trouble getting selected item from combobox into tableview. I have written code for it but it doesn't work or show any exceptions witch is confusing me. But I can't continue because of this problem. The assignment goes: It is necessary to create a form for registering the sale of Internet packages. The Internet package consists of several parameters: speed flow duration of the contract unique identification number name and surname of the user user address.
The flow rate can be 2, 5, 10, 20, 50 and Mbit.
The flow rate can be 1, 5, 10, GB or Flat. The duration of the contract can be one or two years. The identification number is automatically generated when creating a sale. Name and surname and address of the user are entered. It is necessary to provide evidence of creating contracts and sold packages, as follows: review all sales packages adding new sales delete existing sales.
I have a jcombobox containing item1 and item2, also I have a jtextfield. How do I do that? The simple solution would be to use a ItemListener. When the state changes, you would simply check the currently selected item and set the text accordingly.
A better solution would be to create a custom object that represents the value to be displayed and the value associated with it Now I no longer have a 10 month chewing on my ankles, I updated the example to use a ListCellRenderer which is a more correct approach then been lazy and overriding toString. Not an answer to the original question, but an example to the how-to-make-reusable and working custom renderers without breaking MVC It is wrong in a MVC environment, because it is mixing data and view: now the model doesn't contain the data but a wrapper which is introduced for view reasons.
That's breaking separation of concerns and encapsulation every class interacting with the model needs to be aware of the wrapped data. As in Swing a custom renderer is the small coin designed to accomodate for custom visual representation, a default manager which can't cope is Tweaking design just to accommodate for such a crappy default is the wrong way round, kind of upside-down. The correct is, to implement a coping manager. While re-use is fine, doing so at the price of breaking the basic architecture is not a good bargin.
We have a problem in the presentation realm, let's solve it in the presentation realm with the elements designed to solve exactly that problem. As you might have guessed, SwingX already has such a solution In SwingX, the provider of a string representation is called StringValue, and all default renderers take such a StringValue to configure themselves:. As the defaultRenderer is-a StringValue implemented to delegate to the givena well-behaved implementation of KeySelectionManager now can delegate to the renderer to find the appropriate item:.
Outlined the approach because it is easily implementable even without using SwingX, simply define implement something similar and use it:. All except the string provider is reusable as-is that is exactly one implemenation of the custom renderer and the keySelectionManager.
There can be general implementations of the string provider, f. And all without breaking basic rules How are we doing? Please help us improve Stack Overflow. Take our short survey. Learn more. Execute an action when an item on the combobox is selected [closed] Ask Question. Asked 7 years, 1 month ago. Active 6 years, 6 months ago. Viewed k times.