Open image in Swing Application
Hi,
I'm having trouble getting an image to open into my swing application and I cant figure out whats wrong. In response to selecting the "Open" button or menu item a JFileChooser opens up and i select an image but the image doesn't actually load. If anyone could help i'd really appreciate it.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.border.*;
import javax.imageio.*;
class PhotoEditorextends JPanelimplements ActionListener{
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
JMenu imageMenu, effectsMenu;
JFileChooser fc =new JFileChooser();
BufferedImage img =null;
public JMenuBar createMenuBar(){
JMenuBar menuBar =new JMenuBar();
/* Build the first menu: */
JMenu fileMenu =new JMenu("File");
menuBar.add(fileMenu);
fileMenu.setMnemonic(KeyEvent.VK_F);
//a group of JMenuItems under the File option:
String[] menuItems1 ={"Open","Save","Save As..","Close"};
String[] menuIcons1 ={"Open.gif","Save.gif","",""};
for (int i = 0; i<menuItems1.length; i++)
{
JMenuItem fileMI =new JMenuItem(menuItems1[i],new ImageIcon(menuIcons1[i]));
fileMI.addActionListener(this);
fileMenu.add(fileMI);
}
//adding a separator to the drop down menu list
fileMenu.addSeparator();
JMenuItem exitMI =new JMenuItem("Exit",new ImageIcon("Exit.gif"));
exitMI.addActionListener(this);
fileMenu.add(exitMI);
}
/* Code which builds all the menu here */
return menuBar;
}
public JToolBar createToolBar(){
JToolBar toolB =new JToolBar(FlowLayout.LEFT);
toolB.setLayout(new FlowLayout());
// contentPane.add(toolB, "North");
JButton newButton =new JButton(new ImageIcon("new24.gif"));
newButton.addActionListener(this);
toolB.add(newButton);
newButton.setToolTipText("New");
newButton.setActionCommand("New");
//adding a separator to the drop down menu list
toolB.addSeparator();
JButton openButton =new JButton(new ImageIcon("open24.gif"));
openButton.addActionListener(this);
toolB.add(openButton);
openButton.setToolTipText("Open");
openButton.setActionCommand("Open");
/* More code building the toolbar*/
return toolB;
}
publicvoid actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if ((eventSourceinstanceof JMenuItem) || (eventSourceinstanceof JButton));{
String label = (String) e.getActionCommand();
//Sets up the Action Listeners
if (label.equals("Exit")){
System.exit(0);
}
// Closes application
elseif (label.equals("Open")){
openImage();
}
/* More codes for each button or menu item */
}
}
protectedvoid openImage(){
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
try{
img = ImageIO.read(file);
}
catch (IOException e1){
};
}
}
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img, 500, 500,null);
}
privatestaticvoid createAndShowGUI(){
//Create and set up the window.
JFrame frame =new JFrame("Photo Editor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a main label to put in the content pane.
JLabel main =new JLabel();
//main.setOpaque(true);
//main.setBackground(new Color(128, 128, 128));
main.setPreferredSize(new Dimension(800, 600));
//Set the menu bar and add the label to the content pane.
PhotoEditor mainmenu =new PhotoEditor();
frame.setJMenuBar(mainmenu.createMenuBar());
frame.getContentPane().add(mainmenu.createToolBar(), BorderLayout.PAGE_START);
frame.getContentPane().add(main, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
publicstaticvoid main(String[] args){
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowGUI();
}
});
}
}
>

