Java Program to Create a Button and Display Image in the Frame when Clicked

This is a Java Program to Create a Button and Display Image in the Frame when Clicked.

Problem Description

We have to write a program in Java such that it creates a frame with a button. When the button is clicked an image is displayed in the frame.

Expected Input and Output

To display image on click of button, we have the following set of input and output.

To Display the Image on Click:

If an image "logo.jpeg" is available in the present working directory,
then it is expected that the frame displays the image when user clicks on display button.

Sample Image :

Problem Solution

1. Create a frame with a button.
2. Display the frame.
3. When the button is clicked, create an icon of the image using class ImageIcon.
4. Add the icon to a label and then add the label to the frame.

Program/Source Code

Here is source code of the Java Program to display image in frame when button is clicked. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Program to Display Image when Button is Clicked*/
  2. import javax.swing.*;
  3. import javax.swing.ImageIcon.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. class Button_Image implements ActionListener
  7. {
  8. 	static JFrame frame;
  9.         //Driver function
  10. 	public static void main(String args[])
  11. 	{
  12. 		//Create a frame
  13. 		frame=new JFrame("Image on Click");
  14. 		frame.setSize(500,500);
  15. 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. 		frame.getContentPane().setBackground(Color.white);
  17. 		frame.setLayout(new FlowLayout());
  18. 		//Create a button
  19. 		JButton button=new JButton("Display");
  20. 		frame.add(button);
  21. 		//Create an object
  22. 		Button_Image obj=new Button_Image();
  23. 		//Associate ActionListener with button
  24. 		button.addActionListener(obj);
  25. 		//Display the frame
  26. 		frame.setVisible(true);
  27. 	}
  28.         //Function to display image
  29. 	public void actionPerformed(ActionEvent e)
  30. 	{
  31. 		//Display Image
  32. 		ImageIcon icon=new ImageIcon("logo.jpeg");
  33. 		JLabel label=new JLabel(icon);
  34. 		frame.add(label);
  35. 		frame.pack();
  36. 		frame.setSize(500,500);
  37. 	}
  38. }
Program Explanation

1. A frame with a button is created.
2. When the button is clicked, an icon of the image is created using class ImageIcon. The file name of the image is given as a parameter. It is not required to mention the file path is the icon is in the present working directory. Otherwise, complete file path is required.
3. Add the icon to a label and add the label to the frame.
4. Resize the frame after adding the image to the frame as we are using the pack function which may change the size of frame.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases

Here’s the run time test cases to display an image when a button is clicked.

Test case 1 – To display the frame with the button.


Test case 2 – To display the icon in the frame.

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.