Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can JPanel be disposed after the panel has been removed from the JFrame
    primarykey
    data
    text
    <p>I have created 2 Jpanel which will be added to a JFrame.<br> At first one of the JPanel is added to the JFrame.<br> I have used the "add()" method of JFrame to add the JPanel.<br></p> <pre><code>JPanel panel = new JPanel(); JFrame j = new JFrame(); j.getContentPane().add(panel); </code></pre> <p>The JFrame has a JMenuBar set on it.<br> With 2 JMenuItems added to a JMenu which is finally added to the JMenuBar.<br> The 1st JMenuItem when clicked remove the earliar panel from the JFrame and add the other JPanel to the JFrame. The 2nd JMenuItem does the inverse,removing the earliar JPanel and placing the newer JPanel.<br></p> <pre><code>JMenuItem a = new JMenuItem("p1"); a.addActionListener(new... { Frame2 ob = new Frame2();//another class which adds components on the panel. JPanel p1 = ob.getPanel();//method used to return the JPanel from another class j.getContentPane().remove(0); j.getContentPane().add(p1); }); JMenuItem b = new JMenuItem("p2"); a.addActionListener(new... { Frame3 ob2 = new Frame3();//another class which adds components on the panel. JPanel p2 = ob2.getPanel();//method used to return the JPanel from another class j.getContentPane().remove(0); j.getContentPane().add(p2); }); </code></pre> <hr> <p>The problem i face now is that the panels donot get disposed when removed and somewhere in the memory the panels are occupuing memory.<br> Although the previous panel is not visible and the newer panel can be seen, but the memory that the previous panel(not visible panel) is taking up can be seen in the task manager.<br> And as i switch between the panels the memory they ocuppy goes on increasing, as a new instance of the panel is being created every time. I want to dispose the panels when removed, but there is no method to dispose a JPanel like the dispose() method for JFrame.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload