This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox.setSelectedItem() does not update the displayed value

 
Stephan Mueller
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation on JComboBox#setSelectedItem(...) is unusually clear. Have you read it?
 
Stephan Mueller
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:post the relevant parts of your model code: especially getElementAt, getSize, getSelectedItem and setSelectedItem


Please see above.
 
Rob Spoor
Sheriff
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already saw it. You anticipated my request just perfectly
 
Stephan Mueller
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried with DefaultComboBoxModel instead of my own implementation and it show's the same behaviour.
 
Paul Clapham
Sheriff
Pie
Posts: 20184
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, John Correa
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic