Set Up Image Chooser

Learn to load an image from the directory of your choice and to add a filter to the image chooser to display the .jpg files of the directory only.

Reading an image file using the load tool

  1. Add an action listener to loadButton to call load() when the button is clicked. Use JButton’s addActionListener() method, with a new ActionListener object as the parameter value. In the ActionListener, add a public method called actionPerformed(). The method actionPerformed() should take an ActionEvent as a parameter, and return nothing. In actionPerformed(), call load().

Note: Do not execute after adding the load() call in the code. It will have a syntax error because load() does not exist yet.

    ...
 
  public void initGUI() {
 
      ...
    
    JButton loadButton = new JButton(loadIcon);
    loadButton.setToolTipText("Load Image");
    loadButton.addActionListener(__________________ {
      _____________ actionPerformed(_____________) {
        __________();
      }
    });
    toolbar.add(loadButton);
 
      ...

Get hands-on with 1200+ tech skills courses.