This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

itemstatechanged event on Jcombobox  RSS feed

 
Shivani Monga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

I am using SWING GUI builder in Netbeans to create an application. I've added a combo box, comboserver and have added an itemStateChanged event on that which will lead to making another combobox called comboinstance(on which I have yet another itemstatechanged event added) visible on the frame. What next I want to do is to make a label visible on the panel on itemStateChanged event triggered on comboinstance.

In short:
comboserver -- itemstatechanged event (coded to show comboinstance on panel)
comboinstance -- itemstatechanged event( coded to show a label on the panel)

Here's the trouble:

When I click on an item in comboserver, it shows me the comboinstance(which it should, so this is not the problem) and it also shows the label (which is the problem since I want the label to be shown on the event trigger of comboinstance)

HELP!!!

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of ItemListener use ActionListener.
Then you can safely call the setSelectedXXX on the combo box without triggering the listener.
 
Shivani Monga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Maneesh,

I appreciate your reply. But in a previous project I had tried using ActionListener instead of ItemListener on the combobox but it did not work.
 
Amol Katyare
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please share the piece of code?

Guess, you are doing comboinstance.setVisible(true) in the itemstatechange event of comboserver that in turn fires itemstatechange event of comboinstance.

Instead, you can add itemlistener only when you want to show up comboinstance. OR you can play around enablement property of comboinstance.
 
Shivani Monga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Amol

yes i'm using comboinstance.setVisible(true) in the itemstatechange event even though I don't really find this approach very impressive(because I want to allocate memory to a component on the runtime to save memory) and its not also working for me.

What I had in mind earlier was that on the itemstatechangeevent of comboserver
1. Add the comboinstance on the panel.
2. Add the ItemListener to the comboinstance.

I think it would have worked in the above scenario (i'm going to try it now on eclipse) but on netbeans the initComponent() method is invoked from main() wherein the whole code goes automatically when I drag and drop components on the panel from the Swing GUI palette. And to add to it, this initComponent() method is non-editable(i.e. it is greyed out, and doesn't let me modify the code)

I'm not so new to Java but Netbeans is a new environment for me and I can't figure a way out.

If you like I can post you the snapshot of the code.
 
Amol Katyare
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me too not having hands on NetBean's Swing editor. There has to be some option setting that would allow you modify generated code.
 
Shivani Monga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to figure it out.. will update you too if I find something helpful.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> When I click on an item in comboserver, it shows me the comboinstance(which it should, so this is not the problem)
> and it also shows the label (which is the problem since I want the label to be shown on the event trigger of comboinstance)

try it like this

when you run your program:
show comboserver (with items)
show comboinstance (empty - may have to give it a preferredSize, or use setPrototypeDisplayValue())
show label (also empty "" - may have to give it a preferredSize)

a selection in comboserver changes the model of comboinstance
a selection in comboinstance sets the text of the label
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!