Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (9.5k points)

Below is the code I’ve:  

public class Picture extends JFrame  {

    private ImageIcon _image1;

    private ImageIcon _image2;

    private JLabel _mainLabel;

    private JLabel _mainLabel2;

    public Picture(){

        _image1 = new ImageIcon("src/classes/picture1.jpg");

        _image2 = new ImageIcon("src/classes/picture2.jpg");

        _mainLabel = new JLabel(_image1);

        _mainLabel2 = new JLabel(_image2);







 I want to click on the JLabel, which makes a new label with another image attached to it. Can anyone tell me how to do it?

1 Answer

0 votes
by (19.7k points)

You can add mouseListener to the JLable and in the mouseClicked(mouseEvent) method change the icon of JLabel.

Check out the below code implementation: 

  jLabel.addMouseListener(new MouseAdapter() {


        public void mouseClicked(MouseEvent e) {




Interested in Java? Check out this Java Certification by Intellipaat.   

Browse Categories