title: How to use JFileChooser to display image in a JPanel
body: <p>I am trying to use a menubar that enables a user to choose a file and display it in JPanel and image should fit exactly in JPanel. But JFileChooser doesn't display anything upon successfully chosing file from dialog box. I tried refering to many links : <a href="https://stackoverflow.com/questions/299495/java-swing-how-to-add-an-image-to-a-jpanel?rq=1">How to add an image to a JPanel?</a> and 
  <a href="https://stackoverflow.com/questions/13512612/browse-for-image-file-and-display-it-using-java-swing">Browse for image file and display it using Java Swing</a>
   but nothing worked out. Please help. Following is my code:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.io.File;
  
  class Main {
  private JFrame j;
  private JMenu jmenu;
  private JMenuBar jbar;
  private JMenuItem jmi, jexit;
  private JPanel jpanel, jpanelbar;
  private JButton jpre, jnext;
  JLabel image;
  ImageIcon ic;
  Image img;
  
  Main() {
      j = new JFrame("Image Viewer");
      j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // j.setExtendedState(Frame.MAXIMIZED_BOTH);
      // j.setLocationRelativeTo(null);
      j.setLocationByPlatform(true);
      j.setLayout(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      jpanel = new JPanel();
      c.anchor = GridBagConstraints.PAGE_START;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.gridx = c.gridy = 0;
      c.gridwidth = 2;
      // c.weightx=0.1;
      c.weighty = 0.1;
      c.ipady = 600;
      c.insets = new Insets(5, 5, 10, 5);
      // jpanel.setBackground(Color.BLACK);
      j.add(jpanel, c);
  
      jpanelbar = new JPanel();
      jpanelbar.setBackground(Color.red);
      c.weightx = 0.1;
      c.gridx = 0;
      c.gridy = 1;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.insets = new Insets(5, 5, 5, 5);
      c.ipady = 150;
      j.add(jpanelbar, c);
  
      jpanelbar.setLayout(new GridBagLayout());
      GridBagConstraints x = new GridBagConstraints();
      jpre = new JButton("Previous");
      x.gridx = 0;
      x.gridy = 0;
      x.gridwidth = 1;
      x.weightx = 0.1;
      // x.insets=new Insets(5,5,5,5);
      // x.fill=GridBagConstraints.NONE;
      jpanelbar.add(jpre, x);
  
      jnext = new JButton("Next");
      x.gridx = 1;
      jpanelbar.add(jnext, x);
  
      // Creating Menu
      jbar = new JMenuBar();
      jmenu = new JMenu("File");
      jmi = new JMenuItem("Open");
      jmi.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
              JFileChooser fc = new JFileChooser();
              int result = fc.showOpenDialog(null);
              if (result == JFileChooser.APPROVE_OPTION) {
                  File file = fc.getSelectedFile();
                  String sname = file.getName();
                  image = new JLabel("", new ImageIcon(sname), JLabel.CENTER);
                  jpanel.add(image, BorderLayout.CENTER);
              }
          }
      });
      jexit = new JMenuItem("Exit");
      jexit.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
              System.exit(0);
          }
      });
      jmenu.add(jmi);
      jmenu.add(jexit);
      jbar.add(jmenu);
      j.setJMenuBar(jbar);
  
      j.setSize(800, 600);
      j.setResizable(false);
      j.setVisible(true);
  }
  
  public static void main(String s[]) {
      SwingUtilities.invokeLater(new Runnable() {
          public void run() {
              new Main();
          }
      });
  }
  }
  </code></pre>
  
  <p>The updated code as follows:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.io.File;
  
  class Main {
  private JFrame j;
  private JMenu jmenu;
  private JMenuBar jbar;
  private JMenuItem jmi, jexit;
  private JPanel jpanel, jpanelbar;
  private JButton jpre, jnext;
  JLabel image;
  ImageIcon ic;
  Image img;
  
  Main() {
      j = new JFrame("Image Viewer");
      j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // j.setExtendedState(Frame.MAXIMIZED_BOTH);
      // j.setLocationRelativeTo(null);
      j.setLocationByPlatform(true);
      j.setLayout(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      jpanel = new JPanel();
      c.anchor = GridBagConstraints.PAGE_START;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.gridx = c.gridy = 0;
      c.gridwidth = 2;
      // c.weightx=0.1;
      c.weighty = 0.1;
      c.ipady = 600;
      c.insets = new Insets(5, 5, 10, 5);
      // jpanel.setBackground(Color.BLACK);
      j.add(jpanel, c);
  
      jpanelbar = new JPanel();
      jpanelbar.setBackground(Color.red);
      c.weightx = 0.1;
      c.gridx = 0;
      c.gridy = 1;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.insets = new Insets(5, 5, 5, 5);
      c.ipady = 150;
      j.add(jpanelbar, c);
  
      jpanelbar.setLayout(new GridBagLayout());
      GridBagConstraints x = new GridBagConstraints();
      jpre = new JButton("Previous");
      x.gridx = 0;
      x.gridy = 0;
      x.gridwidth = 1;
      x.weightx = 0.1;
      // x.insets=new Insets(5,5,5,5);
      // x.fill=GridBagConstraints.NONE;
      jpanelbar.add(jpre, x);
  
      jnext = new JButton("Next");
      x.gridx = 1;
      jpanelbar.add(jnext, x);
  
      // Creating Menu
      jbar = new JMenuBar();
      jmenu = new JMenu("File");
      jmi = new JMenuItem("Open");
      jmi.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
              JFileChooser fc = new JFileChooser();
              int result = fc.showOpenDialog(null);
              if (result == JFileChooser.APPROVE_OPTION) {
                  File file = fc.getSelectedFile();
                  String sname = file.getName();
                  image = new JLabel("", new ImageIcon(sname), JLabel.CENTER);
                  jpanel.add(image, BorderLayout.CENTER);
                  jpanel.revalidate();
                  jpanel.repaint();
              }
          }
      });
      jexit = new JMenuItem("Exit");
      jexit.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
              System.exit(0);
          }
      });
      jmenu.add(jmi);
      jmenu.add(jexit);
      jbar.add(jmenu);
      j.setJMenuBar(jbar);
  
      j.setSize(800, 600);
      j.setResizable(false);
      j.setVisible(true);
  }
  
  public static void main(String s[]) {
      SwingUtilities.invokeLater(new Runnable() {
          public void run() {
              new Main();
          }
      });
  }
  }
  </code></pre>