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.

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.

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn