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
Some Useful Methods
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, 100, 400, 400, this);
  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(500, 500);
  38. fr.setLocation(200, 200);
  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(1000, 1000);
  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