Reputation: 65
I have a method which returns a jpanel with a layout set like a image carousel. I have checked and this code works fine on a single JPanel
when directly applied. But the same layout is being used in multiple places. Hence I wanted to create a generic method that sets this layout and returns a jPanel
. This is the method I wrote:
public static JPanel loadImages(String Location) throws IOException{
JPanel ImagePanel = new JPanel();
File directory = new File(Location);
ImageIcon image;
String[] imageFiles = directory.list();
DefaultListModel model = new DefaultListModel();
JLabel lab1=new JLabel();
JCheckBox chkbox ;
ImagePanel.setLayout(new GridBagLayout());
for (int ii=0; ii<imageFiles.length; ii++) {
GridBagConstraints constraint = new GridBagConstraints();
image = new ImageIcon(imageFiles[ii]);
lab1 = new JLabel(image);
constraint.gridx = ii;
constraint.gridy =0;
ImagePanel.add(lab1,constraint);
}
for (int ii=0; ii<imageFiles.length; ii++) {
GridBagConstraints constraint1 = new GridBagConstraints();
constraint1.anchor = GridBagConstraints.SOUTH;
chkbox = new JCheckBox(imageFiles[ii]);
constraint1.gridx = ii;
constraint1.gridy =1;
ImagePanel.add(chkbox, constraint1);
}
return ImagePanel;
}
My idea was that in all the places where this is required , I would have a jscrollpane already set in the UI. And I wanted to call this method which would return this JPanel and I wanted to add this to the scroll pane.
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jScrollPane3.add(loadImages("C://users//images"));
jScrollPane3.revalidate();
jScrollPane3.repaint();
}
But nothing is displayed. Is it because the layout is not set?
Upvotes: 0
Views: 97
Reputation: 550
Don't use add on the JScrollPane.
Try:
jScrollPane3.setViewportView(loadImages("C://users//images"));
Upvotes: 1