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();
}