views:

33

answers:

1

Hello,

Some time ago I made the following question about placing multiple images in a JFrame: http://stackoverflow.com/questions/2155596/can-i-create-a-jframe-with-multiple-images. The problem is: When I'm mooving the scrollbar to see the images, I figured out that it consumes a lot of memory (about 1mb each scroll roll for the entire frame). When working with the same JFrame renewed many times (about 100 times in my case), it becomes a problem.

So, I need a help to prevent the high consuption of memory using JFrames.

I've tried to do the same screen using Frame, but it didn't worked at all. Thanks.

PS: The code implemented is relatively simple (it was made in Netbeans) and based on the correct anwer of the other question:

Note, painel is a sub-class of JPanel and draws the image in a JPanel.

ArrayList<painel> panels;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;

/** Creates new form Frame */
public Frame() {
    initComponents();

    panels = new ArrayList<painel>();
    this.setLocationRelativeTo(null);
}

// Load the images in the panel
public void loadImages(ArrayList<String> names) {
    jPanel1.removeAll();
    jScrollPane1.getVerticalScrollBar().setValue(0);
    panels.clear();

    for (String name : names) {
        panels.add(new painel(name));
    }

    for (painel p : panels) {
        jPanel1.add(p);
        p.repaint();
    }

    jPanel1.updateUI();
}

// Generated By NetBeans
@SuppressWarnings("unchecked")
private void initComponents() {
    jScrollPane1 = new javax.swing.JScrollPane();
    jPanel1 = new javax.swing.JPanel();

    jPanel1.setMinimumSize(new java.awt.Dimension(500, 7000));
    jPanel1.setPreferredSize(new java.awt.Dimension(500, 7000));
    jPanel1.setLayout(new javax.swing.BoxLayout(
        jPanel1, javax.swing.BoxLayout.Y_AXIS));
    jScrollPane1.setViewportView(jPanel1);
    jScrollPane1.getVerticalScrollBar().setUnitIncrement(50);

    javax.swing.GroupLayout layout =
        new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(
        javax.swing.GroupLayout.Alignment.LEADING).addComponent(
        jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING,
        javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE));
    layout.setVerticalGroup(layout.createParallelGroup(
        javax.swing.GroupLayout.Alignment.LEADING).addComponent(
        jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING,
        javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE));
    pack();
}
A: 

There is no need for all this repainting and updating UI. I would suggest using JLabel for showing the images instead of JPanel:

  1. Create one JPanel with Vertical BoxLayout (choose any other layout as needed)
  2. Add as many labels to it for each image.
  3. Add panel to the scroll pane.
  4. Set scroll pane's preferable size and add it to the frame.
  5. pack frame and show it.

There is nothing more you need to do here.

One more advice... make sure it is all done on EDT.

Let me know if you want me to put some sample code here.

eugener
What's EDT?@eugener, I'll try it. Regards
marionmaiden
You have to know it of you're using Swing :). It is Event Dispatch Thread - Swing thread. Very important concept in Swing based development.Here are a couple of links for you - read carefully:http://java.sun.com/products/jfc/tsc/articles/threads/threads2.htmlhttp://today.java.net/pub/a/today/2007/08/30/debugging-swing.html
eugener