Save an Image
Learn to save an image after scaling and cropping.
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.
- In
initGUI()
after addingloadButton
to the toolbar, create a newJButton
object calledsaveButton
. withsaveIcon
instead of text. Use theJButton
constructor with anImageIcon
as the parameter value. - Set the tooltip text for
saveButton
to “Save Image” using JButton’ssetToolTipText()
method. - Add an action listener to
saveButton
to callsave()
when the button is clicked. UseJButton
'saddActionListener()
method, with a newActionListener
object as the parameter value. InactionPerformed()
, callsave()
. - Add
saveButton
totoolbar
usingJToolBar
'sadd()
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.