You can shrink down all your images to use less RAM. e.g. this code reduces all images to 200x200. That way you can fit 1000 images in 100MB.
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
public class Scroll extends JPanel {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JPanel panel = new Scroll();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int i = 0; i < 10; i++) {
JPanel buttonPanel = new JPanel();
JRadioButton b1 = new JRadioButton("button 1");
JRadioButton b2 = new JRadioButton("button 2");
JRadioButton b3 = new JRadioButton("button 3");
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
buttonPanel.add(b1);
buttonPanel.add(b2);
buttonPanel.add(b3);
BufferedImage buffer = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffer.createGraphics();
BufferedImage image = ImageIO.read(new File("image.jpg"));
g.scale(buffer.getWidth()*1.0/image.getWidth(),
buffer.getHeight()*1.0/image.getHeight());
g.drawImage(image, 0, 0, null);
g.dispose();
JLabel imageLabel = new JLabel(new ImageIcon(buffer));
JSplitPane splitPane = new JSplitPane();
splitPane.add(imageLabel, JSplitPane.LEFT);
splitPane.add(buttonPanel, JSplitPane.RIGHT);
panel.add(splitPane);
}
JScrollPane spane = new JScrollPane(panel);
frame.add(spane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,600);
frame.setVisible(true);
}
}
If you want to dynamically load images as they become visible you have to use empty JPanels for each image instead of ImageIcons and then you override the paint method of those JPanels. The paint method will only be called if the JPanel is visible. So the simplest solution would be to always load the image from disk in the paint method and then draw it to the screen.
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
public class Scroll extends JPanel {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JPanel panel = new Scroll();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int i = 0; i < 10; i++) {
JPanel buttonPanel = new JPanel();
JRadioButton b1 = new JRadioButton("button 1");
JRadioButton b2 = new JRadioButton("button 2");
JRadioButton b3 = new JRadioButton("button 3");
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
buttonPanel.add(b1);
buttonPanel.add(b2);
buttonPanel.add(b3);
JPanel imagePanel = new JPanel() {
{
BufferedImage image = ImageIO.read(new File("image.jpg"));
setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
image.flush();
}
@Override
public void paint(Graphics g) {
try {
BufferedImage image = ImageIO.read(new File("image.jpg"));
g.drawImage(image, 0, 0, null);
image.flush();
} catch(Exception e) {
}
}
};
JSplitPane splitPane = new JSplitPane();
splitPane.add(imagePanel, JSplitPane.LEFT);
splitPane.add(buttonPanel, JSplitPane.RIGHT);
panel.add(splitPane);
}
JScrollPane spane = new JScrollPane(panel);
frame.add(spane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,600);
frame.setVisible(true);
}
}