In my
previous post I showed how to add a JMenuBar to a Swing JFrame. In this post I will show you how to display a file chooser when clicking one of the items in the menu bar as below.
In this app, when the user selects the 'Open' menu tem from the menu bar then it will allow you to browse a location in your machine to select a file. Once you click on 'Exit' then the program exits.
According to a code below, if the user selects a file or cancels the file selection, a message box will popup accordingly. You can perform any action in the appropriate code blocks as required.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class MenuBarExampleMain extends JFrame implements ActionListener{
private static String OPEN_COMMAND="open";
private static String EXIT_APP_COMMAND="exit";
public MenuBarExampleMain(){
super("Menu Bar Example App"); //sets the title for the JFrame
this.createInterface();
}
public void createInterface(){
setLayout(new BorderLayout()); //main Layout of the JFrame is BorderLayout
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
setSize(xSize,ySize); //setting the JFrame size according to the screen width and height
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(createMenuBar(),BorderLayout.NORTH); //creating a JMenuBar and adding it on top of the frame
add(mainPanel, BorderLayout.CENTER);
setVisible(true);
}
public JMenuBar createMenuBar(){
JMenuBar menuBar=new JMenuBar();
JMenu menuFile=new JMenu("File"); //File,Edit,View and Help are the main menus in the JMenuBar
JMenu menuEdit=new JMenu("Edit");
JMenu menuView=new JMenu("View");
JMenu menuHelp=new JMenu("Help");
JMenuItem subMenuItemOpen=new JMenuItem("Open"); //Open and Exit are the sub menu items under 'File' menu item
subMenuItemOpen.setActionCommand(OPEN_COMMAND);
subMenuItemOpen.addActionListener(this);
JMenuItem subMenuItemExit=new JMenuItem("Exit");
subMenuItemExit.setActionCommand(EXIT_APP_COMMAND);
subMenuItemExit.addActionListener(this);
menuFile.add(subMenuItemOpen);
menuFile.add(subMenuItemExit);
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuView);
menuBar.add(menuHelp);
return menuBar;
}
public static void main(String[] args){
MenuBarExampleMain main=new MenuBarExampleMain();
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (OPEN_COMMAND.equals(command)) {
browseFiles();
}else if (EXIT_APP_COMMAND.equals(command)) {
exitProgram();
}
}
//opens a JFileChooser to browse XML files to parse to the application
public void browseFiles(){
final JFileChooser fileChooser = new JFileChooser();
//allows user to select more than one file
fileChooser.setMultiSelectionEnabled(true);
int returnVal = fileChooser.showOpenDialog(null);
String fileExtension;
//if the user confirms file selection display a message
if (returnVal == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, "User made the selection");
}
else{
JOptionPane.showMessageDialog(null, "User canceled the selection");
}
}
public void exitProgram(){
System.exit(0);
}
}