Results 1 to 6 of 6
  1. #1
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default Removing components from an array

    I am trying to add and remove JTextFields in JPanel whenever a button is pressed. If the JPanel contains any of the JTextField array, that is msg, all the elements of msg should be removed before adding. The adding method works fine. now i want to remove elements. Can someone help me in doing it? I have to somehow search the elements in the JPanel and get its name and delete it.

    Thanks in advance


    Java Code:
    code for adding JTextField
    int n = 0;
    JTextField[] msg= new JTextField[n];
    
    for(int i=0; i<n; i++){           
                msg[i] = new JTextField();
                msgMnemonic[i] = new JTextField();
                panel.add(msg[i]);
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Removing components from an array

    elements of msg should be removed
    The Container class has a method for removing components from the container. Have you tried using it?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default Re: Removing components from an array

    Quote Originally Posted by Norm View Post
    The Container class has a method for removing components from the container. Have you tried using it?
    Yeah, removeAll, remove - by index, remove - by component.

    what i need is removing all the components whose name is msg - that is msg[]. I tried i think it needs Components, If you can give an example it would be very helpful

    something like, panel.remove(msg[i])

    Thanks

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Removing components from an array

    something like, panel.remove(msg[i])
    That looks about right. Have you tried it? What happened?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default Re: Removing components from an array

    Quote Originally Posted by Norm View Post
    That looks about right. Have you tried it? What happened?
    It did not work. It did not delete.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 8
    Last Post: 06-28-2012, 10:46 PM
  2. removing and adding SWING components
    By Athlon* in forum AWT / Swing
    Replies: 1
    Last Post: 12-06-2011, 06:12 PM
  3. Removing characters in an array
    By dru36 in forum New To Java
    Replies: 6
    Last Post: 11-27-2011, 12:56 PM
  4. Adding and removing components from a GridBagLayout
    By peterhabe in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 10:13 PM
  5. Removing components from JPanel
    By Echilon in forum New To Java
    Replies: 0
    Last Post: 12-30-2007, 04:05 PM

Tags for this Thread

Posting Permissions

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