Component not updating??!!
This one has me totally stumped!
I have a class that extends a JDialog and implements the ActionListener interface. It contains JTextField and a JLabel that I am using to display warning messages - e.g. if the user clicks "OK" and they haven't given the right input the JDialog stays visible and the JLabel is changed to show an error message.
Consider the following code in the class that is called to create the warning box and add it to this (which is an extension of JDialog):
Code:
[B]//Example Block 1[/B]
this.m_box_warnings = Box.createVerticalBox();
this.m_box_content = Box.createVerticalBox();
this.m_box_content.add(this.m_box_warnings);
this.getContentPane().add(this.m_box_content);
Consider the following code that is called when actionPerformed is called in response to the user clicking OK:
Code:
[B]//Example Block 2[/B]
this.m_box_warnings.removeAll(); //Get rid of old warning messages.
System.out.println("check 1 :" + this.m_box_warnings.getComponents().length);
for (int n=0; n<warnings.length; n++) //"warnings" is a String[].
this.m_box_warnings.add(new JLabel(warnings[n]));
System.out.println("check 2: " + this.m_box_warnings.getComponents().length);
What happens is this:
Let's say that the first time that Example Block 2 is executed there are three warning messages. The three warnings are displayed on the dialog, as expected, and the outputs from Example Block 2 are as follows:
check 1: 0
check 2: 3
This is what I would expect.
Let's say that the second time Example Block 2 is executed, there are now only two warning messages. The outputs from Example Block 2 are now as follows:
check 1: 0
check 2: 2
This is also what I would expect, BUT ... the problem is that the displayed dialog box still shows the three messages from before!
:confused::confused::confused::confused:
Any ideas?
Thanks in advance!
EDIT:
I fixed it by adding the following code to the end of Example Block 2:
Code:
this.m_box_warnings.setVisible(false);
this.m_box_warnings.setVisible(true);
But I don't understand why this works!!
I'll use it until some kind person explains to me what's going on and suggests a better method.
Please bear with me, I'm just a learner!