Java Program to Create a Toolbar with 3 Push Buttons with Image and Display the Selected Button in the Label

«
»

This is a Java Program to Create a Toolbar with 3 Push Buttons with Image and Display the Selected Button in the Label

Problem Description

We have to write a program in Java such that it creates a toolbar with 3 buttons – New, Open and Print with images. When any button on the toolbar is clicked, the button is displayed in the label.

Expected Input and Output

Consider the following icons are used for the push buttons :-
a) For New Button : This image is saved as “new.jpg”

advertisement

b) For Open Button : This image is saved as “open.jpg”

c) For Print Button : This image is saved as “print.jpg”

For creating a toolbar with push buttons having an image, we can have the following different sets of input and output.

1. To Display the Toolbar:

advertisement
When the program is being executed,
it is expected that a toolbar is created with 3 push buttons having images.
push buttons - New, Open and Print with images

2. To Display the Button clicked: Open

When open button on the toolbar is clicked,
it is expected that a open button is displayed in a label.

3. To Display the Button clicked: New

When new button on the toolbar is clicked,
it is expected that a new button is displayed in a label.

4. To Display the Button clicked: Print

advertisement
When print button on the toolbar is clicked,
it is expected that a print button is displayed in a label.
Problem Solution

1. Create 3 image icons and 3 push buttons. Add image icons to the buttons.
2. Create a toolbar and add the 3 push buttons to it.
3. Add the toolbar to the image.
4. When any button is clicked, create an object of the JButton class to get the button.
5. Obtain the image icon of the button clicked and compare it with the initially set icons.
6. Display the appropriate message.

Program/Source Code

Here is source code of the Java Program to create a toolbar with push buttons having images. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /* Java Program to create a toolbar with push buttons having images*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.image.*;
  6. class Toolbar implements ActionListener
  7. {
  8.     static JLabel text;
  9.     static ImageIcon icon_open,icon_new,icon_print;
  10.     //Driver function
  11.     public static void main(String args[])
  12.     {
  13. 	//Create a frame
  14. 	JFrame frame = new JFrame("Toolbar");
  15. 	frame.setSize(600,400);
  16. 	frame.getContentPane().setBackground(Color.white);
  17. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. 	frame.setLayout(new FlowLayout());
  19. 	//Create an object
  20. 	Toolbar obj =new Toolbar(); 
  21. 	//Create open button
  22. 	icon_open = new ImageIcon("open.jpg");
  23. 	JButton b_open = new JButton(icon_open);
  24. 	b_open.addActionListener(obj);
  25. 	//Create new button
  26. 	icon_new = new ImageIcon("new.jpg");
  27. 	JButton b_new = new JButton(icon_new);
  28. 	b_new.addActionListener(obj);
  29. 	//Create print button
  30. 	icon_print = new ImageIcon("print.jpg");
  31. 	JButton b_print = new JButton(icon_print);
  32. 	b_print.addActionListener(obj);
  33. 	//Create a toobar
  34. 	JToolBar bar = new JToolBar();
  35. 	bar.add(b_open);
  36. 	bar.add(b_new);
  37. 	bar.add(b_print);
  38. 	frame.add(bar);
  39. 	//Create a label
  40. 	text = new JLabel();
  41. 	frame.add(text);
  42. 	//Display the frame
  43. 	frame.setVisible(true);
  44.     }
  45.     //Function to view the button clicked
  46.     public void actionPerformed(ActionEvent e)
  47.     {
  48.         //Create a button of the action event source
  49. 	JButton button = (JButton)e.getSource();
  50. 	//Get the icon of button
  51. 	String icon = button.getIcon().toString();
  52. 	//Display the button clicked
  53. 	if(icon.equals(icon_open.toString()))
  54. 	    text.setText("Button Clicked : Open");
  55.         else if(icon.equals(icon_new.toString()))
  56. 	    text.setText("Button Clicked : New");
  57. 	else
  58. 	    text.setText("Button Clicked : Print");
  59.     }
  60. }
Program Explanation

1. The image icons icon_open, icon_new and icon_print are the icons for the Open, New and Print respectively.
2. Create a toolbar using JToolBar class.
3. To create an button from the action event source, explicitly convert the event source to a button using JButton.
4. Get the string of the image icon of button using getIcon().toString().

advertisement
Runtime Test Cases

Here’s the run time test case to create a toolbar with push buttons having images.

Test case 1 – To View the Toolbar with Push Buttons.
java-program-toolbar-view

Test case 2 – To View the Button Clicked – Open.
java-program-toolbar-button-open

Test case 3 – To View the Button Clicked – New.
java-program-toolbar-button-new

Test case 4 – To View the Button Clicked – Print.
java-program-toolbar-button-print

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
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