Skip to main content

Image Preview in JButton

3 replies [Last post]
eviljoker
Offline
Joined: 2008-11-18
Points: 0

Hi, I'm writing a program to show images preview that are in a particular directories into JButtons but I have two problems:
-if I load an Image bigger than about 3000x4000 px I've received an Heap Space Error... I've resolved this problem with the jvm option -Xmx512m but I think that is not a very good choice...
-If I change directory the buttons will removed from the panel but they stay into the memory so I can receive again an Heap Space Error...

Here a simple program that can explain easier what I want... The difference with my program is that my program uses threads but nothing change about my problems:

public class Main extends JFrame {
JPanel p = new JPanel();
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("File");
JMenuItem mi = new JMenuItem("Apri");
JMenuItem mm = new JMenuItem("Rimuovi");
public Main() {
aggiungiComponenti();
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(fc.showOpenDialog(p) == JFileChooser.APPROVE_OPTION)
azione(fc.getSelectedFile());
}
});
mm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.removeAll();
p.updateUI();
}
});

}
public void aggiungiComponenti() {
add(p);
mb.add(m);
m.add(mi);
m.add(mm);
setJMenuBar(mb);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
}
public void azione(File dir) {
if(p.getComponentCount() != 0) {
p.removeAll();
p.updateUI();
}
File f[] = dir.listFiles();
ImageIcon icons[] = new ImageIcon[f.length];
for (int i = 0; i < f.length; i++) {
System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);
icons[i] = new ImageIcon(f[i].getAbsolutePath(), f[i].getName());
ImageIcon adactIcon = new ImageIcon(getScaledImage(icons[i].getImage(), 100, 100));
JButton thumbButton = new JButton(adactIcon);
thumbButton.setAlignmentX(JButton.CENTER_ALIGNMENT);
p.add(thumbButton);
p.revalidate();
}
}
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}

}

Message was edited by: eviljoker

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
eviljoker
Offline
Joined: 2008-11-18
Points: 0

sorry here is the code

[code]
public class Main extends JFrame {
JPanel p = new JPanel();
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("File");
JMenuItem mi = new JMenuItem("Apri");
JMenuItem mm = new JMenuItem("Rimuovi");
public Main() {
aggiungiComponenti();
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(fc.showOpenDialog(p) == JFileChooser.APPROVE_OPTION)
azione(fc.getSelectedFile());
}
});
mm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.removeAll();
p.updateUI();
}
});

}
public void aggiungiComponenti() {
add(p);
mb.add(m);
m.add(mi);
m.add(mm);
setJMenuBar(mb);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
}
public void azione(File dir) {
if(p.getComponentCount() != 0) {
p.removeAll();
p.updateUI();
}
File f[] = dir.listFiles();
ImageIcon icons[] = new ImageIcon[f.length];
for (int i = 0; i < f.length; i++) {
System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);
icons[i] = new ImageIcon(f[i].getAbsolutePath(), f[i].getName());
ImageIcon adactIcon = new ImageIcon(getScaledImage(icons[i].getImage(), 100, 100));
JButton thumbButton = new JButton(adactIcon);
thumbButton.setAlignmentX(JButton.CENTER_ALIGNMENT);
p.add(thumbButton);
p.revalidate();
}
}
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}

}
[/code]

I've tried your suggestion but nothing is changed; I've also tried to create JButton's and ImageIcons's array outside azione's method but nothing is changes.

eviljoker
Offline
Joined: 2008-11-18
Points: 0

I paste the second part of the program...

icons[i] = new ImageIcon(f[i].getAbsolutePath(), f[i].getName());
ImageIcon adactIcon = new ImageIcon(getScaledImage(icons[i].getImage(), 100, 100));
JButton thumbButton = new JButton(adactIcon);
thumbButton.setAlignmentX(JButton.CENTER_ALIGNMENT);
p.add(thumbButton);
p.revalidate();
}
}
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}

}

walterln
Offline
Joined: 2007-04-17
Points: 0

Use [ code ] code here [ /code ] tags without the spaces to post code.

Do you call button.setIcon(null);? Should not really be nessecary if you remove the button though.