I am trying to get 20 images displayed on a jframe in jlabels using a gridlayout. I have added a while loop but still i am only am to see 1 image, please can you help me to display more than image in jlabels on a jframe?

setSize(600, 600);
setDefaultCloseOperation(HIDE_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(new GridLayout(4,4));
connection = getConnection();
try { // table name:image and second image is field name
statement = (PreparedStatement) connection
.prepareStatement("select content from  images");
result = statement.executeQuery();
byte[] image = null;

while (result.next()) {
image = result.getBytes("image");
}
Image img = Toolkit.getDefaultToolkit().createImage(image);
img = img.getScaledInstance(100, 200, Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(img);
JLabel lPhoto = new JLabel();
lPhoto.setIcon(icon);
stultuske commented: seriously? +13

you only wrote code to display one single image, what did you think would happen?

Image img = Toolkit.getDefaultToolkit().createImage(image);
img = img.getScaledInstance(100, 200, Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(img);
JLabel lPhoto = new JLabel();
lPhoto.setIcon(icon);

you only get the last image, because you read them all, override each previous value, and use the last to set the image.

stresstedout: why did you (yet again) downvote my post? because I answered your question?
if you don't want people to answer your questions, or explain what you're doing wrong ... here's a radical thought: don't ask questions.

stultuske: downvotes without a comment are, by design, anonymous, so beware of assuming you know who voted.
However, stressedout overstepped the mark by re-posting the same question, then posting two entries containing personal abuse, so I have banned him. This thread is therefore dead.
JC

considering every reply on one of his threads got downvoted, ... pretty obvious.
chances of someone else going to all the trouble of searching out one persons threads and downvoting each and every one reply in it, not of the OP's, are generally very slim.
true, there is the oft chance, but I doubt it. if not, he could have replied in his previous thread that he wasn't the one downvoting.
anyway, I posted my previous reply before I noticed his 'banned' status, otherwise, I wouldn't have.

commented: You are right +14

btw whats the question ...........

None, now. stresstedout got too stressed, turned abusive, and got banned. This thread is dead.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.