title: Show image by click JButton
body: <p>I have a problem. I don't know how to display an image by clicking a JButton. </p>
  
  <p>I have a class which can show and hide an image:</p>
  
  <pre><code>/**
   * 
   */
  package com.samples;
  
  import java.awt.BorderLayout;
  import java.awt.Container;
  import java.awt.EventQueue;
  import java.awt.FlowLayout;
  import java.awt.Graphics;
  import java.awt.Image;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.ImageIcon;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.WindowConstants;
  
  /**
   * @author
   *
   */
  public class New2 extends JFrame implements ActionListener {
  
      private static String SHOW_ACTION = "show";
      private static String HIDE_ACTION = "hide";
  
      private Image image = null;
      private boolean showImage = false;
  
      public New2(String filename) {
          setTitle("MyWindow");
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          setSize(800, 600);
  
          this.image = new ImageIcon("..//src/img/Ster.png").getImage();
  
          Container container = getContentPane();
          container.setLayout(new BorderLayout());
          container.add(createControls(), BorderLayout.SOUTH);
      }
  
      private JPanel createControls() {
          JButton showButton = new JButton("Show");
          showButton.addActionListener(this);
          showButton.setActionCommand(SHOW_ACTION);
  
          JButton hideButton = new JButton("Hide");
          hideButton.addActionListener(this);
          hideButton.setActionCommand(HIDE_ACTION);
  
          JPanel panel = new JPanel();
          panel.setLayout(new FlowLayout(FlowLayout.CENTER));
  
          panel.add(showButton);
          panel.add(hideButton);
  
          return panel;
      }
  
      @Override
      public void paint(Graphics g) {
          super.paint(g);
  
          if (showImage) {
              g.drawImage(image, 100, 200, image.getWidth(null), image.getHeight(null), null);
          }
      }
  
      @Override
      public void actionPerformed(ActionEvent event) {
          String actionCommand = event.getActionCommand();
  
          if (SHOW_ACTION.equals(actionCommand)) {
              showImage = true;
          } else if (HIDE_ACTION.equals(actionCommand)) {
              showImage = false;
          }
  
          repaint();
      }
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
  
              @Override
              public void run() {
                  New2 frame = new New2("resources/image.jpg");
                  frame.setVisible(true);
              }
          });
      }
  }
  </code></pre>
  
  <p>I'm working with MVC so I want the code for the JButton in my map for controllers but I don't know how to do this. </p>
  
  <pre><code>package View;
  
  import java.awt.Color;
  import java.awt.Container;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.Image;
  import java.awt.event.ActionListener;
  
  import javax.swing.BorderFactory;
  import javax.swing.ImageIcon;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JMenu;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  import javax.swing.JPanel;
  
  
  import Controller.HomeController;
  import Controller.KeeperController;
  
  public class Selectie extends JFrame{
  
      private JLabel label, label1, label2;
      private JButton keeper;
      private JPanel panel;
      private Container window = getContentPane();
      private KeeperController controller;
  
  
      public Selectie()
      {
          initGUI();
  
      }
  
      public void initGUI()
      {
          setLayout(null);
          setTitle();
          setSize(800,600);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          label = new JLabel();       
          label.setBounds(0, 0, 266, 800);
          label.setBackground(Color.RED);
          label.setOpaque(true);
          window.add(label);
  
          label1 = new JLabel();
          label1.setBounds(266, 0, 266, 800);
          label1.setBackground(Color.BLACK);
          label1.setOpaque(true);
          window.add(label1);
  
          label2 = new JLabel();
          label2.setBounds(532, 0, 266, 800);
          label2.setBackground(Color.RED);
          label2.setOpaque(true);
          window.add(label2);
  
          keeper = new JButton("1. "+""+" Kenneth Vermeer");
          keeper.setBounds(60, 500, 200, 25);
          keeper.setFocusable(false);
          keeper.setBorderPainted(false);
          keeper.setContentAreaFilled(false);
          keeper.setFont(new Font("Arial",Font.PLAIN,17));
          label.add(keeper);
  
          }
  
  }
  </code></pre>
  
  <p>The button keeper needs to show the image when it is clicked.</p>