I have this code, and it makes a JPanel and adds it to a JScrollPanel. It works fine, but when I try to add a second JPanel it removes the first one and adds the second one. I would like to be able to place JPanels on top of JPanels, how can I do that?
// Location of an image:
String file = wfc.getSelectedFile().getPath();
// Creates images from different types:
ImageHandler image = new ImageHandler();
BufferedImage imageData = image.imageData(file);
// Extends JPanel, Layer is a JPanel
Layer layer = new Layer(image.width(), image.height());
layer.setImage(imageData);
layer.setSizeFromLoaded();
// A list of all the JPanels added
Layers.set(Layers.layers.size(), layer);
// Adds a JPanel to the JScrollPanel
imagePane.getViewport().add(layer, BorderLayout.CENTER);
Here is the full method, it opens a file browser, then when the image is selected it runs the above code
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
WebFileChooser wfc = null;
if(wfc == null){
wfc = new WebFileChooser(this, "Open an Image");
wfc.setSelectionMode(SelectionMode.SINGLE_SELECTION);
wfc.setAvailableFilter(GlobalConstants.IMAGES_AND_FOLDERS_FILTER);
wfc.setChooseFilter(GlobalConstants.IMAGES_FILTER);
wfc.setCurrentDirectory("/Users");
}
wfc.setVisible(true);
if(wfc.getResult() == StyleConstants.OK_OPTION){
String file = wfc.getSelectedFile().getPath();
ImageHandler image = new ImageHandler();
BufferedImage imageData = image.imageData(file);
Layer layer = new Layer(image.width(), image.height());
layer.setImage(imageData);
layer.setSizeFromLoaded();
Layers.set(Layers.layers.size(), layer);
imagePane.getViewport().add(layer, BorderLayout.CENTER);
}
}
I used Zove Games suggestion and placed it within a JLayerdPanel, and it works perfectly!