1

I am trying to load an image into a JPanel using JFileChooser. But when I try to run the program and load a selected image nothing happens in the JPanel. I am attaching the source code snippet here:

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
FileFilter filter = new FileNameExtensionFilter("Image files","jpeg","jpg");  
fileChooser.setFileFilter(filter);  
int result = fileChooser.showOpenDialog(null);    
if(result == fileChooser.APPROVE_OPTION){  
imgFile = fileChooser.getSelectedFile();//imgFile is File type    
try{  
    myPicture = ImageIO.read(imgFile);//myPicture is BufferedImage  
    JLabel picLabel = new JLabel(new ImageIcon( myPicture )) ;  
    imagePanel.add( picLabel );  
    imagePanel.repaint();  
    System.out.println("You have selected "+imgFile);  
    }catch(Exception e){  
         e.printStackTrace();  
    }  
    }  

}

Can anyone shed light on this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user976754
  • 361
  • 1
  • 4
  • 19
  • For better help sooner, post an [SSCCE](http://sscce.org/). *"after .. shifted away from its position .. How to restrict this(?)"* Layouts. More info. when I see the SSCCE. – Andrew Thompson Apr 28 '12 at 10:27
  • @user976754: You can use the example cited [here](http://stackoverflow.com/a/10362719/230513) as the basis of your [sscce](http://sscce.org/). – trashgod Apr 28 '12 at 10:36

4 Answers4

2

The problem is that I have added two panels in my frame.

You might compare what you're doing with this complete example that uses two panels: a file chooser on the left and a display panel on the right.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I think this might help you...

Object selectedItem = jComboBox14.getSelectedItem(); 
ImageIcon picturetoInsert = new ImageIcon(selectedItem.toString()); 
JLabel label = new JLabel("", picturetoInsert, JLabel.CENTER); 
JPanel panel = new JPanel(new GridLayout(1, 1));
panel.add(label, BorderLayout.CENTER); 
jInternalFrame22.getContentPane(); 
jInternalFrame22.setContentPane(panel); 
jInternalFrame22.setVisible(true);
Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41
abc
  • 11
  • 1
0

Why don you try with the paint component?

class imagePanel extends JPanel
{ 
  BufferedImage image;
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    if(image != null)
    {
      g.drawImage(image, 0, 0, this);
    }
  }
}
Lucky
  • 111
  • 2
  • 14
  • Actually imagePanel is an object of JPanel. I can invoke the paintComponent method of it but what will be the arguement for Graphics g in it.... – user976754 Apr 28 '12 at 09:41
0

There could be a few reasons for this. You could try

imagePanel.invalidate()

before the repaint call to force it to redraw.

Or possibly the label is to small and needs resizing since there may not have been an image before. You could try invoke the

frame.pack();

method to get the frame to recompute its component sizes.

Or you could try force the size of the label (setting its min size) to ensure it has enough space to show the image.

Bruce Lowe
  • 6,063
  • 1
  • 36
  • 47
  • Thanks both metohds are working and i am able to load the image. But the problem is that I have added two panels in my frame. I want to load the image within the boundary of imagePanel. but here, after loading the image the second panel is shifted away from its position and the imagePanel occupies the most frame position. How to restrict this. – user976754 Apr 28 '12 at 09:49
  • always tricky to get layouts right. you could use set max size on one of the 1st panel to prevent it shifting too much and put the image panel inside a jscrollpane - that would mean if the image gets too large it will introduce scrollbars. Or you could load a placeholder image into the 1st panel before the user has selected an image so that the gui doesnt move once one is selected and again use the jscrollpane in case the user picks an image which is too large (else scale it down using some java libs) – Bruce Lowe Apr 28 '12 at 13:09