Updating a JDialog after the user does something

I'm writing a program that (among other things) creates a JDialog.

On the JDialog there is a JLabel and a JComboBox.
When the user selects something different in the JComboBox, the text on the JLabel needs to change.
What code do I need to write in the listener for the JComboBox to do this?
(The dialog is actually a lot more complicated that this, so I'm hoping that I don't have to dispose of the dialog and draw it again.)
peparsonsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
yourCombo.addActionListener( new ActionListener() {

     public void actionPerformed(ActionEvent evt) {
          // update your label
     }
});
0
CEHJCommented:
Make your JDialog implement ItemSelectionListener and have it update the JLabel
0
zzynxSoftware engineerCommented:
More precise:

yourCombo.addActionListener( new ActionListener() {

     public void actionPerformed(ActionEvent evt) {
          JComboBox combo = (JComboBox)evt.getSource();
          String selectedText = (String)combo.getSelectedItem();
          yourLabel.setText(selectedText);
         
          // if "yourLabel" is in the JPanel called myPanel
          myPanel.revalidate();
          myPanel.repaint();
     }
});
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

CEHJCommented:
class MyDialog extends JDialog implements ItemListener {
              // This method is called only if a new item has been selected.
              public void itemStateChanged(ItemEvent evt) {
                  JComboBox cb = (JComboBox)evt.getSource();
          
                  // Get the affected item
                  Object item = evt.getItem();
          
                  if (evt.getStateChange() == ItemEvent.SELECTED) {
                      // Item was just selected
                      
                      // Set the label
                      
                      label.setText(item.toString());
                      
                  } else if (evt.getStateChange() == ItemEvent.DESELECTED) {
                      // Item is no longer selected
                  }
              }
   
}
0
CEHJCommented:
(Obviously you need to add the listener (the dialog) to the JComboBox)
0
zzynxSoftware engineerCommented:
Both our solutions work. You have the choice.
0
CEHJCommented:
... so your code would include something like this:

class MyDialog extends JDialog implements ItemListener {
      private JComboBox myCombo;
      
      public MyDialog() {
            myCombo = new JComboBox();
            myCombo.addItemListener(this);
            
            // now add the component
      }
      
}
0
peparsonsAuthor Commented:
Thanks for your speedy response. The problem is solved, I think.

I needed to put some code into the listener to change the text on the label, using the same method (and algorithm) that was used when it was created. What it's meant is that I've had to separate the setting of text on the label from the creation of the label. (I think that's what MVC is about.)

Also, I found that it didn't matter whether revalidate() and repaint() were called or not.

If nobody comments on this in the next day or so, I'll split the points.

Thanks again.
0
CEHJCommented:
>>
I needed to put some code into the listener to change the text on the label, using the same method (and algorithm) that was used when it was created.
>>

So you could probably call the same method to do this each time
0
peparsonsAuthor Commented:
>>
So you could probably call the same method to do this each time
>>

That's what I've done.
0
zzynxSoftware engineerCommented:
>> If nobody comments on this in the next day or so, I'll split the points.
Sounds fair
0
CEHJCommented:
8-)
0
zzynxSoftware engineerCommented:
peparsons,

Didn't you say you would split the points?
That sounded fair, but not this way... Did you make a mistake?
Can you please comment on this?
0
peparsonsAuthor Commented:
I could have made a mistake....

I intended to split the 500 points equally, giving zzynx and CEHJ 250 points each.
Didn't I do it correctly?
0
peparsonsAuthor Commented:
Ooops - I get your point. Sorry!
How do I correct it?
0
zzynxSoftware engineerCommented:
I'll ask the page editor to reopen this one. Just wait.
Once reopened you can re-accept.
0
zzynxSoftware engineerCommented:
Thanks for this correction peparsons
0
CEHJCommented:
8-) And thank *you* zzynx ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.