How to Upload Images in Java Applications

Introduction

 
In this article, we are going to describe how to upload your image into a frame, and image selection with the help of a FileDailog. File choosers provide a GUI for navigating the file system and then either choose a file or directory from a list or enter the name of a file or directory. To display a file chooser, you usually use the JFileDailog API to show a modal dialog containing the file chooser. Another way to present a file chooser is to add an instance of JFileDailog to a container.
 
Note
 
If you want to start an unsigned Java™ Web application as a distributed form then you should use the services of JNLP instead of jFileChooser. The services FileOpenService and FileSaveService not only provide support for choosing files in a restricted environment but also take care of actually opening and saving them.  An example of using these services is in JWSFileChooserDemo. Documentation for using the JNLP API can be found in the Java Web Start lesson.
 

FileDailog Class API Description

 
Constructor Details
  • FileDialog(Dialog parent) 

     This constructor is used to create a file dialog for loading a file. Here parent is the owner of the dialog

  • FileDialog(Dialog parent, String title) 

    This constructor is used to create a file dialog window with the specified title for loading a file. Here parent is the owner of the dialog and the second argument title is the title of the dialog; a null value will be accepted without causing a NullPointerException to be thrown.

  • FileDialog(Dialog parent, String title, int m)

    This constructor is used to create a file dialog window with the specified title for loading or saving a file. Here the parent is the owner of the dialog and the second argument title is the title of the dialog. And the third is the mode of the dialog; either FileDialog.LOAD or FileDialog.SAVE.
Some Useful Methods
  • void addNotify()
    Creates the file dialog's peer.

  • String getDirectory()
    Gets the directory of this file dialog.

  • String getFile() 
    Gets the selected file of this file dialog.

  • FilenameFilter getFilenameFilter() 
    Determines this file dialog's filename filter.

  • File[] getFiles() 
    Returns files that the user selects.

  • int getMode()
    Indicates whether this file dialog box is for loading from a file or for saving to a file.

  • boolean isMultipleMode() 
    Returns whether the file dialog allows the multiple file selection.

  • protected String paramString()
    Returns a string representing the state of this FileDialog window.

  • void setDirectory(String dir) 
    Sets the directory of this file dialog window to be the specified directory.

  • void setFile(String file)
    Sets the selected file for this file dialog window to be the specified file.

  • void setFilenameFilter(FilenameFilter filter) 
    Sets the filename filter for this file dialog window to the specified filter.

  • void setMode(int mode) 
    Sets the mode of the file dialog.

  • void setMultipleMode(boolean enable) 
    Enables or disables multiple file selection for the file dialog.
Example
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.awt.image.*;  
  4. import javax.swing.*;  
  5. import java.awt.Frame;  
  6. import java.awt.event.WindowAdapter;  
  7. import java.awt.event.WindowEvent;  
  8. class imageLoad extends Canvas  
  9. {  
  10.  Image img;  
  11.  public imageLoad(Image img)  
  12.  {  
  13.   this.img = img;  
  14.  }  
  15.  public void paint(Graphics g)  
  16.  {  
  17.   if (img != null)  
  18.   {  
  19.    g.drawImage(img, 100400400this);  
  20.   }  
  21.  }  
  22.  public void setImage(Image img)  
  23.  {  
  24.   this.img = img;  
  25.  }  
  26. }  
  27. public class ImagesLoading implements ActionListener  
  28. {  
  29.  JFrame fr = new JFrame("Image loading program Using awt");  
  30.  Label Label1 = new Label("Choose your image");  
  31.  Button Button1 = new Button("select");  
  32.  Image Image1;  
  33.  imageLoad Canvas1;  
  34.  FileDialog fd = new FileDialog(fr, "Open", FileDialog.LOAD);  
  35.  void initialize()  
  36.  {  
  37.   fr.setSize(500500);  
  38.   fr.setLocation(200200);  
  39.   fr.setBackground(Color.lightGray);  
  40.   fr.setLayout(new FlowLayout());  
  41.   fr.add(Label1);  
  42.   fr.add(Button1);  
  43.   fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  44.   Button1.addActionListener(this);  
  45.   Canvas1 = new imageLoad(null);  
  46.   Canvas1.setSize(10001000);  
  47.   fr.add(Canvas1);  
  48.   fr.show();  
  49.  }  
  50.  void imageload()  
  51.  {  
  52.   fd.show();  
  53.   if (fd.getFile() == null)  
  54.   {  
  55.    Label1.setText("You have not select");  
  56.   } else  
  57.   {  
  58.    String d = (fd.getDirectory() + fd.getFile());  
  59.    Toolkit toolkit = Toolkit.getDefaultToolkit();  
  60.    Image1 = toolkit.getImage(d);  
  61.    Canvas1.setImage(Image1);  
  62.    Canvas1.repaint();  
  63.   }  
  64.  }  
  65.  public void windowClosing(WindowEvent e)  
  66.  {  
  67.   System.exit(0);  
  68.  }  
  69.  public void windowActivated(WindowEvent e)  
  70.  {  
  71.  }  
  72.  public void windowClosed(WindowEvent e)  
  73.  {  
  74.  }  
  75.  public void windowDeactivated(WindowEvent e)  
  76.  {  
  77.  }  
  78.  public void windowDeiconified(WindowEvent e)  
  79.  {  
  80.  }  
  81.  public void windowIconified(WindowEvent e)  
  82.  {  
  83.  }  
  84.  public void windowOpened(WindowEvent e)  
  85.  {  
  86.  }  
  87.  public void actionPerformed(ActionEvent event)  
  88.  {  
  89.   Button b = (Button) event.getSource();  
  90.   if (b == Button1)  
  91.   {  
  92.    imageload();  
  93.   }  
  94.  }  
  95.  public static void main(String args[])  
  96.  {  
  97.   ImagesLoading a = new ImagesLoading();  
  98.   a.initialize();  
  99.  }  
  100. }  
Output
 
cmd output
 
 tdcmd.gif
 
Initial Output
 
td1.gif
 
When you click on the select button the following window will appear.
 
img2.png
 
After browsing a selected image the following will be shown:
 
td2.gif
 
Resources 


Similar Articles