We will need to be able to save the image after it has been scaled or cropped.

Adding a save tool to the toolbar

Next, add similar code to add a “Save” tool button to the toolbar.

  1. In initGUI() after adding loadButton to the toolbar, create a new JButton object called saveButton. with saveIcon instead of text. Use the JButton constructor with an ImageIcon as the parameter value.
  2. Set the tooltip text for saveButton to “Save Image” using JButton’s setToolTipText() method.
  3. Add an action listener to saveButton to call save() when the button is clicked. Use JButton's addActionListener() method, with a new ActionListener object as the parameter value. In actionPerformed(), call save().
  4. Add saveButton to toolbar using JToolBar's add() method.

Note: Do not execute the code yet. It will have a syntax error because save() does not exist yet.

    ...
 
  public void initGUI() {
 
      ...
 
    toolbar.add(loadButton);
    
    ____________ saveButton = __________________;
    saveButton.__________________;
    saveButton.addActionListener(_______________() {
      ________________ actionPerformed(________________) {
        _____________();
      }
    });
    toolbar.________________;
 
    ...

Get hands-on with 1200+ tech skills courses.