Java Program to Create a Menu and Handle the File Open Event for the User

«
»

This is a Java Program to Create a Menu and Handle the File Open Event for the User

Problem Description

We have to write a program in Java such that it creates a frame with a menu item to handle the File Open event.
By File Open event it means that the user shall be able to browse the system and select a file. The file path of the selected file will be displayed in the frame.

Expected Input and Output

For handling the File Open event, we can have the following set of input and output.

advertisement

To display the file selected:

When any file item is selected from the file browser window,
it is expected that the complete file path is displayed in the frame.
Problem Solution

1. Create a Menu with the Open menu item.
2. When the user clicks on Open, a file browser window is opened.
3. The path of file selected is displayed in the frame.

advertisement
Program/Source Code

Here is source code of the Java Program to handle the file open event. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /* Java Program to handle the File Open Event*/
  2. import javax.swing.*;
  3. import javax.swing.filechooser.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. class Open_File implements ActionListener
  7. {
  8.     static JFrame frame;
  9.     static JLabel label;
  10.     //Driver function
  11.     public static void main(String args[])
  12.     {
  13. 	//Create a frame
  14. 	frame = new JFrame("Open a File");
  15. 	frame.setSize(500,500);
  16. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. 	frame.setLayout(new FlowLayout());
  18. 	//Create a label
  19. 	label = new JLabel();
  20. 	frame.add(label);
  21. 	//Create an object
  22. 	Open_File obj = new Open_File();
  23. 	//Create a Menu
  24. 	JMenu menu = new JMenu("File");
  25. 	//Create a Menu Item
  26. 	JMenuItem open = new JMenuItem("Open");
  27. 	open.addActionListener(obj);
  28. 	menu.add(open);
  29. 	//Create a menu bar
  30. 	JMenuBar mb=new JMenuBar();
  31. 	mb.add(menu);
  32. 	frame.setJMenuBar(mb);
  33. 	//Display the frame
  34. 	frame.setVisible(true);
  35.     }
  36.     //Function to create a file chooser and display the path
  37.     public void actionPerformed(ActionEvent e)
  38.     {
  39. 	//Create a file chooser
  40. 	JFileChooser file = new JFileChooser();
  41. 	file.showOpenDialog(null);
  42. 	label.setText("File : "+file.getSelectedFile());
  43.     }
  44. }
Program Explanation

1. To create a file chooser use the JFileChooser class.
2. To view the file chooser, use showOpenDialog method.
3. Display the complete path of file.

advertisement
Runtime Test Cases

Here’s the run time test case to handle the file open event.

Test case – To view the path of the file.
java-program-handle-file-open

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn