Results 1 to 6 of 6
  1. #1
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default 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):

    Java 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:

    Java 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:

    Java 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!
    Last edited by XmisterIS; 09-02-2010 at 05:13 PM. Reason: Fixed it, but I don't understand the fix

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    What does this.m_box_warnings.revalidate() do?

    kind regards,

    Jos

  3. #3
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    Well, TBH it doesn't seem to do a lot! I put it in above the setVisible true/false lines, then I commented out the setVisible lines .. and not a lot happened!

    It still seems to work only with the setVisible bit of code.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by XmisterIS View Post
    Well, TBH it doesn't seem to do a lot! I put it in above the setVisible true/false lines, then I commented out the setVisible lines .. and not a lot happened!

    It still seems to work only with the setVisible bit of code.
    Well, we have to wait for Camickr or Darryl then; they're real Swing wizards ;-) and I don't know much about Swing. The best of luck with this problem; I'll keep on reading this thread because I'm convinced it could be solved 'cheaper'.

    kind regards,

    Jos

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    The standard idiom is that after adding components to a visible container you need to call revalidate() and after removing components you need to call both revalidate() and repaint() on the container.

    Since you are both removing and adding components, a single
    Java Code:
    revalidate();
    repaint();
    at the end will suffice.

    Your requirement does look as if using a JList would be easier all round.

    To get better help sooner post a SSCCE
    SSCCE : Java Glossary

    db

  6. #6
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    Many thanks Darryl!

    I will familiarise myself with JList too.

Similar Threads

  1. Updating a .jar File
    By Kyx in forum New To Java
    Replies: 3
    Last Post: 08-20-2010, 03:20 AM
  2. Updating JTabel
    By drwk in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 12:52 PM
  3. Updating Arrays
    By drymsza1234 in forum New To Java
    Replies: 3
    Last Post: 12-06-2009, 02:34 PM
  4. Updating my GUI
    By Catkill in forum AWT / Swing
    Replies: 6
    Last Post: 09-01-2009, 05:09 PM
  5. Updating into 2 tables in the DB
    By yuchuang in forum New To Java
    Replies: 2
    Last Post: 05-12-2007, 06:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •