How can I have the components displayed while executing the given code, as JList elements.
What I have done in this code is that, I have created different JPanels and to those JPanels, added an icon with a corresponding name, and finally added all these JPanels to a main JPanel(as you can see from the code).
I am looking for JList alternative instead of JPanels for the reason that, I want to select/deselect these icons as in a windows explorer, which I think, is possible with a JList. How to modify this code such that I can achieve this? The alignment of the icons should be like that in current code.
Any help is greatly appreciated.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.filechooser.FileSystemView;
import sun.awt.shell.ShellFolder;
public class ScrollableWrapTest {
public static void main(String[] args) {
try {
ScrollableWrapTest st = new ScrollableWrapTest();
final JPanel mainPanel = new JPanel(new WrapScollableLayout(FlowLayout.LEFT, 10, 10));
mainPanel.setBackground(Color.WHITE);
JScrollPane pane = new JScrollPane(mainPanel);
pane.setPreferredSize(new Dimension(550, 300));
Vector v = new Vector();
v.add("first.doc");
v.add("second.txt");
v.add("third.pdf");
v.add("fourth.rar");
v.add("fifth.zip");
v.add("sixth.folder");
v.add("seventh.exe");
v.add("eighth.bmp");
v.add("nineth.jpeg");
v.add("tenth.wav");
JLabel img = null;
for(int i =0;i<v.size();i++){
JPanel panel = new JPanel(new BorderLayout());
Icon icon = st.getIcone(st.getExtension(v.elementAt(i).toString().toUpperCase()));
panel.setBackground(Color.WHITE);
img = new JLabel(icon);
img.setBackground(Color.WHITE);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
buttonPanel.add(img);
JLabel label = new JLabel((String)v.elementAt(i));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setSize(new Dimension(5,5));
panel.add(buttonPanel , BorderLayout.NORTH);
panel.add(label, BorderLayout.SOUTH);
panel.addMouseListener(new MouseAdapter(){});
mainPanel.add(panel);
mainPanel.addMouseListener(new MouseAdapter(){
});
mainPanel.revalidate();
}
st.buildGUI(pane);
} catch (Exception e) {e.printStackTrace();}
}
public void buildGUI(JScrollPane scrollPane)
{
JFrame frame = new JFrame("Scrollable Wrap Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public String getExtension(String name)
{
if(name.lastIndexOf(".")!=-1)
{
String extensionPossible = name.substring(name.lastIndexOf(".")+1, name.length());
if(extensionPossible.length()>6)
{
return "";
}
else
{
return extensionPossible;
}
}
else return "";
}
public Icon getIcone(String extension)
{
File file;
String cheminIcone = "";
if(((System.getProperties().get("os.name").toString()).startsWith("Mac")))
cheminIcone = System.getProperties().getProperty("file.separator");
else if(((System.getProperties().get("os.name").toString()).startsWith("Linux")))
cheminIcone = "/"+"tmp"+"/BoooDrive-"+System.getProperty("user.name")+"/";
else cheminIcone = System.getenv("TEMP") + System.getProperties().getProperty("file.separator");
File repIcone = new File(cheminIcone);
if(!repIcone.exists()) repIcone.mkdirs();
try
{
if(extension.equals("FOLDER"))
{
file = new File(cheminIcone + "icon");
file.mkdir();
}
else
{
file = new File(cheminIcone + "icon." + extension.toLowerCase());
file.createNewFile();
}
Icon icone = getSystemIcon(file);
file.delete();
return icone;
}
catch (IOException e){ }
return null;
}
public static Icon getSystemIcon(File f) {
if (f != null) {
Class fsv = FileSystemView.class;
try {
Method m = fsv.getDeclaredMethod("getShellFolder", new Class[]{File.class});
m.setAccessible(true);
ShellFolder sf = (ShellFolder) m.invoke(FileSystemView.getFileSystemView(), f);
Image img = sf.getIcon(true);
if (img != null) {
return new ImageIcon(img, sf.getFolderType());
} else {
return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
}
} catch (Exception e) {e.printStackTrace();}
}
return null;
}
private static class WrapScollableLayout extends FlowLayout {
public WrapScollableLayout(int align, int hgap, int vgap) {
super(align, hgap, vgap);
}
public Dimension preferredLayoutSize(Container target) {
synchronized (target.getTreeLock()) {
Dimension dim = super.preferredLayoutSize(target);
layoutContainer(target);
int nmembers = target.getComponentCount();
for (int i = 0 ; i < nmembers ; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
dim.height = Math.max(dim.height, d.height + m.getY());
}
}
if (target.getParent() instanceof JViewport)
dim.width = ((JViewport) target.getParent()).getExtentSize().width;
Insets insets = target.getInsets();
dim.height += insets.top + insets.bottom + getVgap();
return dim;
}
}
}
}