• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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.)
0
peparsons
Asked:
peparsons
  • 7
  • 7
  • 4
2 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now