Results 1 to 2 of 2
  1. #1
    Gorgro is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Getting an IllegalArgumentException when using the replace() function

    I'm using grouplayout and I'm trying to switch a Jpanel on and off. I tried using the replace() function, but it only works to display it the first time. When I try to replace it with an empty frame again, I get an Exception and the line "Component must already exist", which is confusing because both Components that I'm working with have definitely been initialised. This is the code that executes when a button is clicked: (panelOn is initialised as false at the start of the application)

    JPanel ATPanel = new ATPanel();
    if(panelOn == false){
    panelLayout.replace(fillerPanel, ATPanel);
    panelOn = true;
    }else{
    panelLayout.replace(ATPanel, fillerPanel); < Error occurs here
    }

    I haven't got much experience with Swing yet, so any help is greatly appreciated.

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

    Default

    The GroupLayout replace method entry in the Java API will explain what will cause the IllegalArgumentException to be thrown when replace is called:

    if either of the components are null or existingComponent is not being managed by this layout manager
    So since you know for a fact that both components are not null (and I'd check this out with a println statement or two), then the other condition must be occurring: The ATPanel is not currently being managed by that layout when the method is called.

    Myself, I don't use GroupLayout but rather several of the other more human-friendly layout managers.

    Luck!

Similar Threads

  1. proper use of IllegalArgumentException
    By vendetta in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 08:43 PM
  2. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  3. Replies: 1
    Last Post: 08-07-2009, 10:37 PM
  4. Replies: 12
    Last Post: 05-26-2009, 02:48 PM
  5. Replies: 1
    Last Post: 08-06-2007, 07:04 PM

Posting Permissions

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