Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Preventing Java Heap memory error using Swing framework and abstract classes
    primarykey
    data
    text
    <p>Really hope you can assist. I have built a stock management application with the Java Swing framework. This application has 1 abstract class named GUIRules which extends JPanel implements actionListener, and 8 different classes which extend GUIRules to represent each screen.</p> <pre><code>public abstract class GUIRules extends JPanel implements ActionListener{} public class login extends GUIRules {} </code></pre> <p>The fact is all of these 8 classes have a lot in common; hence the reason I decided to provide each one of them with default functionality. </p> <p>When I navigate to each screen, all loads up fine; however, I eventually encounter Java Heap memory error. This is the method which enables the changing of screen:</p> <pre><code> /** * When switching between GUI screens, declare new GUI within parameters * @param panel */ public void changePanel(JPanel panel){ this.removeAll(); this.add(panel); validate(); setVisible(true); } </code></pre> <p>this.removeAll() is supposed to remove all components, thereby freeing memory. All action listeners are added within the constructors of all the 8 screens; so this should also get disposed of when this function is called. </p> <p>This application also utilises images as well. </p> <p>The abstract class (GUIrules) calls paintComponent() to load the background image:</p> <pre><code> /** * Paint background, menu and logo images onto this class. */ public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image.mainImg, 0, 0,getWidth(),getHeight(),null); g.drawImage(image.menuImg, 0, 30, getWidth(), 80, null); g.drawImage(image.logoImg, 0, 33, null); g.setColor(Color.CYAN); g.drawString(SystemSession.user, 10, 20); g.drawString(SystemSession.status, 10, getHeight()-10); repaint(); } </code></pre> <p>I have used Runtime.gc() to garbage collect unused objects but not much difference. Any ideas? Many thanks.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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