This is a Java Program to Create a Toolbar with 3 Push Buttons with Image and Display the Selected Button in the Label
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.
Consider the following icons are used for the push buttons :-
a) For New Button : This image is saved as “new.jpg”
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:
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
When print button on the toolbar is clicked, it is expected that a print button is displayed in a label.
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.
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.
/* Java Program to create a toolbar with push buttons having images*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
class Toolbar implements ActionListener
{
static JLabel text;
static ImageIcon icon_open,icon_new,icon_print;
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame = new JFrame("Toolbar");
frame.setSize(600,400);
frame.getContentPane().setBackground(Color.white);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
//Create an object
Toolbar obj =new Toolbar();
//Create open button
icon_open = new ImageIcon("open.jpg");
JButton b_open = new JButton(icon_open);
b_open.addActionListener(obj);
//Create new button
icon_new = new ImageIcon("new.jpg");
JButton b_new = new JButton(icon_new);
b_new.addActionListener(obj);
//Create print button
icon_print = new ImageIcon("print.jpg");
JButton b_print = new JButton(icon_print);
b_print.addActionListener(obj);
//Create a toobar
JToolBar bar = new JToolBar();
bar.add(b_open);
bar.add(b_new);
bar.add(b_print);
frame.add(bar);
//Create a label
text = new JLabel();
frame.add(text);
//Display the frame
frame.setVisible(true);
}
//Function to view the button clicked
public void actionPerformed(ActionEvent e)
{
//Create a button of the action event source
JButton button = (JButton)e.getSource();
//Get the icon of button
String icon = button.getIcon().toString();
//Display the button clicked
if(icon.equals(icon_open.toString()))
text.setText("Button Clicked : Open");
else if(icon.equals(icon_new.toString()))
text.setText("Button Clicked : New");
else
text.setText("Button Clicked : Print");
}
}
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().
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.
Test case 2 – To View the Button Clicked – Open.
Test case 3 – To View the Button Clicked – New.
Test case 4 – To View the Button Clicked – Print.
Sanfoundry Global Education & Learning Series – Java Programs.
If you find any mistake above, kindly email to [email protected]- Practice Programming MCQs
- Practice BCA MCQs
- Practice Information Technology MCQs
- Check Java Books
- Apply for Computer Science Internship