开发者

Jpanel with multiple images - Memory Usage

开发者 https://www.devze.com 2023-01-06 22:17 出处:网络
Some time ago I made the following question about placing multiple images in a JFrame: Can I create a JFrame with multiple images?.

Some time ago I made the following question about placing multiple images in a JFrame: 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.swi开发者_运维百科ng.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();
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消