I have searched many places to add and display images dynamically on JPanel but couldn't get proper help.
Basically I have JPanel on which I have to display many images vertically but it should be dynamic.
for(int i=0;i
JLabel jLabel = new JLabel(new ImageIcon(macthedImages[i]));
searchResultPanel.add(jLabel);
}
macthedImages is an array of bufferedImages
searchResultPanel is JPanel
解决方案
If you want to show all images at same time then use GridLayout but you have to consider rows and columns of grid layout.
GridLayout gl = new gridLayout(2,macthedImages.length/2);
Or if you want to show one image at a time then use CardLayout. Like this:
CardLayout cl = new CardLayout();
for(int i=0;i
JLabel jLabel = new JLabel(new ImageIcon(macthedImages[i]));
cl.add(jLabel, "jLabel"+i);
}
In second option you can show any image by firing event. It provides many methods