We help IT Professionals succeed at work.
Get Started

How can I add a buffered image to my jframe

dyarosh
dyarosh asked
on
358 Views
Last Modified: 2014-05-11
I am new to Java and am trying to learn how to add objects to my window.  My window has a menu bar with an Open option on it.  When Open is clicked, I use Java's FileChooser to allow the user to choose an image to display.  I have the code working so the image is read into a BufferedImage.  What I haven't been able to figure out is how to display the image in my window.  Here is what I have tried but the image doesn't display in the window.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;



public class ImageEditor extends JFrame implements ActionListener
{
   public static BufferedImage image = null;
     
   public ImageEditor()
   {
      super("ImageEditor");
      
   }
   
   public BufferedImage readImage(String fileName) throws IOException
   {
      File file = new File(fileName);
      return ImageIO.read(file);
   }

   public void actionPerformed(ActionEvent e) 
   {
      System.out.println(e.getActionCommand());
      switch(e.getActionCommand())
      {
         case "Open":
            JFileChooser chooser = new JFileChooser();
            //FileFilter filter = new FileNameExtensionFilter(
             //  "JPG & GIF Images", "jpg", "gif");
            //chooser.setFileFilter(filter);
            chooser.setMultiSelectionEnabled(false);
            int returnVal = chooser.showOpenDialog(this);
            if(returnVal == JFileChooser.APPROVE_OPTION) {
               System.out.println("You chose to open this file: " +
                  chooser.getSelectedFile().getPath());
               try
               {
                  image = readImage(chooser.getSelectedFile().getPath().toString());
                  System.out.println(image.getWidth());
                  System.out.println(image.getHeight());
               }
               catch(IOException x)
               {
                  
               }
               JLabel jLabel = new JLabel(new ImageIcon(image));
               JPanel jPanel = new JPanel();
               jPanel.add(jLabel);
               this.add(jPanel);
               super.repaint();
            }
            break;
            
         case "Exit":
            System.exit(0);
            break;
      }
      
   }

   public JMenuBar createMenuBar() 
   {
      JMenuBar menuBar;
      JMenu menu,menu2;
      JMenuItem menuItem,menuItem2;
   
        //Create the menu bar.
      menuBar = new JMenuBar();
   
        //Build the first menu.
      menu = new JMenu("File");
      menu.setMnemonic(KeyEvent.VK_A);
      menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
      menuBar.add(menu);
      
      menu2 = new JMenu("Image");
      menu2.setMnemonic(KeyEvent.VK_A);
      menu2.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
      menuBar.add(menu2);
   
   
        //a group of JMenuItems
      menuItem = new JMenuItem("Open", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Increase box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu.add(menuItem);
   
      menuItem = new JMenuItem("Save", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu.add(menuItem);
      
      menuItem = new JMenuItem("Exit", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu.add(menuItem);
      
      menuItem = new JMenuItem("makeRed", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeGreen", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeBlue", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeColorsInverted", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeGreyScale", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeWarhol", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("flipHorizontal", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("flipVertical", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("rotateClockwise", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      return menuBar;
   }
   
   public static void main(String[] args)
   {
      ImageEditor window = new ImageEditor();
      window.setJMenuBar(window.createMenuBar());
      window.setBounds(100, 100, 800, 600);
      window.setDefaultCloseOperation(EXIT_ON_CLOSE);
      window.setVisible(true);
   }
}

Open in new window


Any help is greatly appreciated!
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE