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
- Add an action listener to
loadButton
to callload()
when the button is clicked. Use JButton’saddActionListener()
method, with a newActionListener
object as the parameter value. In theActionListener
, add a public method calledactionPerformed()
. The methodactionPerformed()
should take anActionEvent
as a parameter, and return nothing. InactionPerformed()
, callload()
.
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.