Let me explain the project I'm creating. I need a list of images, like a film strip, where I can drag the image from the strip to another part of the program and do things with it. That is the ultimate goal, however as of right now I'm just working my way up. I created a FilmStripItem which stores the image information and displays the thumbnail of the image. I'm trying to create a FilmStripPanel which is a JScrollPane that shows each of the thumbnails in a Y-Axis form.
Now I'm new to Java Swing and I've been banging my head against the wall for weeks trying to figure out BorderLayout, BoxLayout, GridLayout and which one would be best. On top of that I can't seem to get any of them to work the way I want. Below is the code I'm using (I got rid of the unnecessary details of the class). Basically what happens is when I run the FilmStripPanel it squishes all the images to fit the window size and no scrollbar shows up. How can I get it to display the images (80x80) without squishing them?
FilmStripItem.java
//Removed import info to save space
public class FilmStripItem extends JPanel {
private BufferedImage testImg;
private Image thumbnail;
public FilmStripItem(BufferedImage img) {
this.testImg = img;
this.thumbnail = img.getScaledInstance(80, 80, Image.SCALE_SMOOTH);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.setMinimumSize(new Dimension(80, 80));
add(p);
}
public Image GetThumbnail() {
return this.thumbnail;
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(this.thumbnail, 0, 0, null);
}
public static void main(String[] ARGS) {
try {
//Removed look and layout info
} catch (Exception ex) {
Logger.getLogger("Demo").log(Level.WARNING,
"loading CuiLookAndFeel failed", ex);
}
JFrame f = new JFrame("FilmStripItem");
final FilmStripItem fi = new FilmStripItem();
f.setContentPane(fi);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(10, 10, 128, 128);
f.setVisible(true);
}
}
FilmStripPanel.java
//import info removed to save space
public class FilmStripPanel extends JPanel {
private static final int ITEM_COUNT = 15;
private ArrayList<FilmStripItem> filmItems;
public FilmStripPanel() {
JPanel p = new JPanel(new GridLayout(0, 1));
setLayout(new GridLayout(1, 1));
filmItems = new ArrayList<FilmStripItem>();
JComponent[] c = new JComponent[ITEM_COUNT];
for (int i = 0; i < ITEM_COUNT; i++) {
FilmStripItem item = new FilmStripItem();
filmItems.add(item);
c[i] = item;
p.add(c[i]);
}
JScrollPane sp = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(sp);
}
public static void main(String[] args) {
try {
//Removed look and feel info
} catch (Exception ex) {
Logger.getLogger("Demo").log(Level.WARNING,
"loading CuiLookAndFeel failed", ex);
}
JFrame f = new JFrame("FilmStrip");
final FilmStripPanel p = new FilmStripPanel();
f.setContentPane(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(10, 10, 128, 512);
f.setVisible(true);
}
}
I have googled for this over and over again but can't seem to figure it out for my exact problem. I'm sure there is some redundant code in here and place feel free to point it out. I could use all the help you guys could offer and any criticism as well, I'm trying to learn Java Swing for work and am failing.