File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JComboBox.setSelectedItem() does not update the displayed value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox.setSelectedItem() does not update the displayed value" Watch "JComboBox.setSelectedItem() does not update the displayed value" New topic
Author

JComboBox.setSelectedItem() does not update the displayed value

Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
Hi.

Consider the following code (trimmed):



When I manually call setSelectedItem(item) I can see it's working as the actionPerformedEvent() method is invoked.
The problem is, that the ComboBox does not display the value of the selected item but still displays the same value as it was before I've invoked
setSelectedEntity().
"setSelectedItem()" is introduced for a loader-mechanism, that populates my gui with pre-defined values. The general user-interaction approach to the JComboBox
is still used and works as expected.

I've tried with repaint()/revalidate()/etc. but my Swing-Fu is obviously not good enough to understand what is going on here.
Please, point me into the right direction on what I'm missing here.


1. Make it run - 2. Make it run correctly - 3. Make it pretty OR fast/small - 4. ??? - 5. Profit
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Is the display name in the model? I believe that JComboBox allows objects that are not part of its contents to be set as the selected item. Verify with a loop:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

The documentation on JComboBox#setSelectedItem(...) is unusually clear. Have you read it?


luck, db
There are no new questions, but there may be new answers.
Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
Yes, I've read it.

If <code>anObject</code> is in the list, the display area shows
<code>anObject</code> selected.


I've used Rob's loop (which seems to be almost the same as the one from JComboBox) to check if the Item (a String) I'm going to set to selected is already in
the list and the result is as expected, the item can be found with a valid index of 3.

I'm using my own ComboBoxModel where I took parts out of the DefaultComboBoxModel (namely the setSelectedItem() method)
but I can't see where the flaw is.
Maybe I'm moving back to Swing's default implementation and convert my values to feed the model and see, if the error is gone.

Here's the code for the Model.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You're right, I was mistaken. The selected item is not updated when setSelectedItem is called with an element not in the combo box. It's DefaultComboBoxModel that allows the selected item to be different from any of its elements.

Stephan, I think it's time you post the relevant parts of your model code: especially getElementAt, getSize, getSelectedItem and setSelectedItem. I am starting to think the problem can be found in one of these methods.

Edit: I see you already posted them. And I also see that you've essentially copied DefaultComboBoxModel. I can't see any problems with that, to be honest. Can you add some debug statements in the setSelectedItem and getSelectedItem methods?
Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
Rob Prime wrote:post the relevant parts of your model code: especially getElementAt, getSize, getSelectedItem and setSelectedItem


Please see above.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

I already saw it. You anticipated my request just perfectly
Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
I've tried with DefaultComboBoxModel instead of my own implementation and it show's the same behaviour.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Then that says that the object you are passing to setSelectedItem() is not equals() to any of the objects already in the model.

I didn't see any equals() methods in your post, but it's possible I missed seeing it. Anyway I think that's where you should look now.
Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
Alright. As usual, the problem was in front of the monitor.
I had a flawed logic for the process of setting the selected Item by the application.
The problem didn't appear here, because it is in a different part of the application.
This part is responsible for initializing the comboBox. There's been an additional call to this init() method that occured after I called
selectedItem(). So selectedItem actually worked, but my init() method trashed this comboBox and created a new one.

The reason behind this: I'm using an EventBus to and the Event I'm sending to set the selected item was accidentally the same as the event to initialize the ComboBox.

Thanks for bothering!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

Stephan Mueller wrote:Yes, I've read it.

If anObject is in the list, the display area shows anObject selected.

I was referring to this:
If anObject is not in the list and the combo box is uneditable, it will not change the current selection. For editable combo boxes, the selection will change to anObject.
Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
Sure, but that made no difference. The Object (String) I wanted to insert was clearly contained in the ComboboxModel.
If this is the case, then it doesn't matter if it is a mutable ComboBox or not as the item should be selected.
The equals.() method for String worked as expected but to be 100% sure, I've also tried by retrieving the actual object from the model and pass it to setSelectedItem().
But for reasons pointed out, that was all to no avail
John Correa
Greenhorn

Joined: Feb 22, 2013
Posts: 1
I think the specifications in the JComboBox aren't very clear. This issue can be resolved by referring to the combo box's object model.

comboBox.getModel().setSelectedItem( object );
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Welcome to the Ranch, John Correa
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JComboBox.setSelectedItem() does not update the displayed value