#Deleted JPanel is still being drawn to the screen JSwing

1 messages · Page 1 of 1 (latest)

full osprey
#

I am trying to delete and add JPanels through code but when I delete a panel with remove() it still gets drawn to the screen and takes up a slot on my list.

brazen talonBOT
#

<@&987246487241105418> please have a look, thanks.

full osprey
#
private void deletePanel(OrderLinePanel panel) {
        JPanel currentJPanel = panel.getOrderLinePanel();
        
        currentJPanel.removeAll();
        remove(currentJPanel);
        orderLinePanels.remove(panel);
        revalidate();
        repaint();
    }
#

When I debug I can see the JPanel becomes null. But it remains on the screen

#

My program automatically adds to the 5 if you add the same one twice

#

but if you hit the delete button and try again

#

This happens

true echo
full osprey
#

The problem is remove() doesn't delete it

true echo
#

but you said the component list is empty after deleting, yeah?

full osprey
true echo
# full osprey

so deleting pushes the component to the center? im trying to piece together the story

#

or are you saying the gap above the new component is the old component that wont be removed?

full osprey
#

Deleting it does nothing. The removeall just makes it invisible

true echo
#

and when you add a new panel, your container shows 2 components? or just 1?

#

when debugging

full osprey
#

They all show up when adding

true echo
#

but when deleting, do they all still show up?

full osprey
#

Yes

true echo
#

check the reference IDs within the debugger, see if currentPanel matches the ID of the components in your OrderLinePanel

full osprey
#

Are you talking about this id?

#

Because if so then they aren't the same

true echo
#

yup

#

which means the panel you're getting from OrderLinePanel isn't actually one of the panels on the container

full osprey
#

Why can I use removeAll on it then?

true echo
#

it could be that there's some intermediate panel

#

check the components of the panel you're trying to remove, see if that contains a JPanel

#

the currentJPanel may contain the panel you are trying to remove

full osprey
#

It seems to be contained inside

true echo
#

hence why removeAll would remove the components (since its removing the panel), but can't be used to remove from the root container

full osprey
#

But thats what its suppossed to

#

OrderLinePanel is a wrapper class i made for it

#

So I could link it with other variables to make it easier to code

#

the JPanel itself doesnt have a JPanel inside of it

true echo
#

is OrderLinePanel an actual JPanel?

full osprey
#

No

#
package gui;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OrderLinePanel {
    private JPanel orderLinePanel;
    private JTextField quantityField;
    private String name;
    private int quantity;
    
    public OrderLinePanel(JPanel orderLinePanel, JTextField quantityField, String name, int quantity) {
        this.orderLinePanel = orderLinePanel;
        this.quantityField = quantityField;
        this.name = name;
        this.quantity = quantity;
    }
    
    public int getQuantity() {
        return quantity;
    }
    
    public String getName() {
        return name;
    }
    
    public JPanel getOrderLinePanel() {
        return orderLinePanel;
    }

    
    public void setQuantity(int quantity) {
        this.quantity = quantity;
        quantityField.setText(Integer.toString(quantity));
    }
    
    public void setName(String name) {
        this.name = name;
    }
}
#

The naming is a little confusing. I def need to fix it up

true echo
#

what im saying is check if the orderLinePanel (which you're trying to remove) has a parent or a JPanel child

full osprey
#

It doesnt

true echo
#

whats the parent?

full osprey
#

The parent is a another jpanel

true echo
#

so compare the IDs of all those panels, see which one matches the ID of the panel that's added to the list container

full osprey
#

They are the same

true echo
#

which one matches though?

full osprey
true echo
#

before you said currentJPanel didnt have an ID that matched the panel added to the list

full osprey
#

I took this screenshot before deleting

full osprey
#

Maybe deleting it does something

true echo
full osprey
#

They are 100% the same

true echo
full osprey
#

I must have misread it

#

Strange

#

They 100% have the correct ids

#

I have triple checked

full osprey
#

@true echo ?

true echo
# full osprey <@281666014754897921> ?

i'd have to check it out. all im seeing are pieces of the problem. i still believe that, based on the behavior of removeAll and remove you're seeing, that there is an intermediate panel that's causing trouble

#

im at work right now though, so it would have to be later, either during a break or when i get home

#

mind posting the whole project, via github or some other repo hosting site?

full osprey
#

its school policy

#

But I can give you the source code for the entire class thats causing problems

#

you can just comment out any backend stuff

#

and add a main method

true echo
full osprey
#

Are you available rn?

full osprey
brazen talonBOT
full osprey
#
package gui;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OrderLinePanel {
    private JPanel orderLinePanel;
    private JTextField quantityField;
    private String name;
    private int quantity;
    
    public OrderLinePanel(JPanel orderLinePanel, JTextField quantityField, String name, int quantity) {
        this.orderLinePanel = orderLinePanel;
        this.quantityField = quantityField;
        this.name = name;
        this.quantity = quantity;
    }
    
    public int getQuantity() {
        return quantity;
    }
    
    public String getName() {
        return name;
    }
    
    public JPanel getOrderLinePanel() {
        return orderLinePanel;
    }

    
    public void setQuantity(int quantity) {
        this.quantity = quantity;
        quantityField.setText(Integer.toString(quantity));
    }
    
    public void setName(String name) {
        this.name = name;
    }
}
true echo
brazen talonBOT
#

@full osprey

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

full osprey
#

.

true echo
full osprey
#

Yes, it doesnt work

#

I have tried everything i can think off.

floral monolith
#

ignore the message i wrote, i confused something with something lol

true echo
brazen talonBOT
#

@full osprey

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

true echo
#

you instead try removing from the current instance

#

instead of remove(currentJPanel), you should remove it from the panel you added it to: centerOf0L.remove(currentJPanel)

full osprey
#

I thought remove() worked on the jframe itself and not on each individual component

#

Works now, appreciate it