aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes When removing several JPanels the last JPanel remains, anyone have this issue? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "When removing several JPanels the last JPanel remains, anyone have this issue?" Watch "When removing several JPanels the last JPanel remains, anyone have this issue?" New topic
Author

When removing several JPanels the last JPanel remains, anyone have this issue?

Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Hi All,

I'm trying to create a dynamic way for a user to insert data to be saved and edited.
So I have a JPanel which will store a vertical list of fields.
Each field is it's own JPanel with 4 JComponents inside: JCheckBox, 2 JLabels, and a JTextField.
The user can add additional fields (JPanels) into the Jpanel holding the entire list of fields by clicking an "ADD" button.
(Up to here all works fine.)
The user should also be able to delete individual fields that have been selected using that fields associated JCheckbox by clicking the "DELETE" button.
(Here's where I'm stumped and need another pair of eyes to help find what causing the following issue)
When I check several fields JCheckBoxes and click the "DELETE" button, all the selected fields accept the last selected field are deleted from the GUI.
I've tried using both remove() and removeAll() and I have the same issue.
I've also tried adding in an extra revalidate() with no favorable outcome as well.

Here's a look at my code for this:


Since I don't know how or if it's posible to attached an image here, I'll try to illustrate best I can a before and after graphic.

before clicking the "DELETE" button:

[X] <[deleting this one 1]>
[ ] <[saving this one 1]>
[X] <[deleting this one 2]>
[ ] <[saving this one 2]>
[X] <[deleting this one 3]>

What I end up with after clicking the "DELETE" button:

[ ] <[saving this one 1]>
[ ] <[saving this one 2]>

[X] <[deleting this one 3]>

Please let me know if any of you have experience and found a solution for this.
Or if you've never come across this but have an idea to what causes it and a possible solution.

Much thanks for any help rendered.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Call revalidate() and repaint(). In that order.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Lets say you have 3 panels - 0, 1, 2

In your loop you remove panel 0. All the panels shift down 1 so you know have panels 0, 1.

Then your loop removes panel 1. You are still left with panel 0.

So whenever you want to delete something from a List or an array you need to start at the end and count down to 0.
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Thanks Maneesh,

That seem to work, but it did bring to light some other unexplained behavior.
Now the DELETE button when clicked will remove all the checked panels, but it sometimes takes either a couple clicks
or just waiting for something to refresh before it completes. Do you think this is a thread issue, any sugestions?

Hi Rob,

Thanks for your sugestion, this was looked into as well and found not to be the culprit in this case.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Joe McTigue wrote:Do you think this is a thread issue, any sugestions?

Yup. To understand why this is happening and how to solve it, check out http://docs.oracle.com/javase/tutorial/uiswing/concurrency/
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
I doubt it is a Thread issue. Your code is executing in a listener. All listener code executes on the EDT.

this was looked into as well and found not to be the culprit in this case.


Did you just "look into it" or actually change the code?

Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Hi Rob,

I changed the code just after I posted it to correct the order in which I deleted from arraylist and panels in gui.
Here's what I changed it to:



works a lot better but still doesn't delete every checked panel on first 'delete' button click.

Any ideas?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Any ideas?


I gave you my idea. The code you posted doesn't not do what I suggested. I suggested:

you need to start at the end and count down to 0.


You are starting at 0 and counting up.
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Actually Rob,

I did try that as well with a for loop that went like the following:



This had no change...
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Hi Rob,

Thanks for being persistant... :-)
Yes, this is me eatting crow. ;-)

turns out the suggestion you made works if you tweak it a slight bit.


Thanks again folks!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Then the standard response is to post your SSCCE which demonstrates the problem. Actually this is the first thing you should do when you ask a question. Most of the time when you create the SSCCE you find the problem yourself. We can't keep guessing what the problem might be.
 
Don't get me started about those stupid light bulbs.
 
subject: When removing several JPanels the last JPanel remains, anyone have this issue?