• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

repainting issue

 
shihab K Bava
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JLabel on a JScrollPane. I am trying to load images in to the label as icon. The file names of the images are selected by a file chooser. My issue is when I load the image first time it shows correctly. But when I am trying to load another image, the 2nd image is not displaying. But first image is displaying there. Could you please check the code below and tell me where should do modification?

------------------------ Code Part ---------------------------
String imgName = chooser.getSelectedFile().getPath();
System.out.println(imgName);

ImageIcon imgIcon = new ImageIcon(imgName);

JLabel imgLabel = new JLabel(imgIcon);

setTitle("BrowseImage - " + imgName);

imagePane = new JScrollPane();
//imagePane.removeAll();
imagePane.add(imgLabel);
imagePane.revalidate();
imagePane.setViewportView(imgLabel);

imgLabel.setHorizontalAlignment(JLabel.CENTER);
imgLabel.setVisible(true);

getContentPane().add(imagePane,BorderLayout.NORTH);
imagePane.setSize(screenWidth-200,screenHeight-200);
imagePane.setLocation(100,50);

imgLabel.revalidate();

getContentPane().repaint();

setVisible(true);

--------------------------------------------------------
Thanks in advance
Shihab KB
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JScrollPane can only have one child component. Try using a JPanel that is the child component, then add your labels to that JPanel.

Also, please Please Use Code Tags.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading this as though you have a single label
and just want to show another image in that label.

if so, all you need is (after showing the original image)

String imgName = chooser.getSelectedFile().getPath();
originalLabel.setIcon(new ImageIcon(imgName));
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic