How to add an event to an icon?

Asked

Viewed 279 times

0

I wanted to make it possible to open a screen, perform any action and so by clicking on an icon.

I wanted to put different formats, so I don’t use one JButton. Because when passing the image to him, he remained "rectangular". Furthermore, in case there is any component that can take the form of an image or even make with Jbutton, it is also valid.

Example click on a small circle:

inserir a descrição da imagem aqui

package pacote01;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class NovoClass extends JFrame
{    
    private JPanel painel = new JPanel();

    public NovoClass()
    {
        setSize(200, 150);
        ImageIcon image = new ImageIcon(getClass().getResource("/imagens/icone.png"));   
        JLabel imagelabel = new JLabel(image); 

        painel.add(imagelabel);
        add(painel);   
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);      
    }

    public static void main(String[] args) {
        NovoClass novo = new NovoClass();
    }
}

1 answer

1


To add a listener in a jLabel you can use the addMouseListener, passing as a parameter an implementation of MouseListener called MouseAdapter, making it over-written only in the method mouseClicked.

Below is the section responsible for this:

imagelabel.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        System.out.println("clicou no icone");
    }
});

Below is the complete code

public class NovoClass extends JFrame {
    private JPanel painel = new JPanel();

    public NovoClass() {
        setSize(200, 150);
        ImageIcon image = new ImageIcon(getClass().getResource("/imagens/icone.png"));
        JLabel imagelabel = new JLabel(image);
        //Adiciona o mouse listener
        imagelabel.addMouseListener(getMouseListener());

        painel.add(imagelabel);
        add(painel);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    //Cria o mouse listener
    public MouseListener getMouseListener() {
        //Utiliza a implementação do mouseAdapter
        return new MouseAdapter() {
            //Faz a sobre-escrita apenas no click
            public void mouseClicked(MouseEvent e) {
                System.out.println("clicou no icone");
            }
        };
    }

    public static void main(String[] args) {
        NovoClass novo = new NovoClass();
    }
}
  • first thank you ! Just for "knowledge", you can tell me if a Jbutton or other component takes the form of an icon ?

  • 1

    Not really, as close as you can get to putting an icon inside JButton by the method setIcon in this way: jbutton.setIcon(image);. But it’s just a button with a centered icon

  • Right, it’s just that I wanted to change the mouse pointer, plus, from what I just read, it looks like it’s just overwriting a certain method, when I go over the icone.

  • 1

    Cool, in reality you can change the mouse cursor when passing over a JLabel, you can use the method setCursor and add the desired cursor to jLabel in this way: imagelabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));, this example it leaves the cursor in Jlabel with a little hand (Cursor.HAND_‌​CURSOR), further into the Cursor you can select another template from the ones that are already preset

  • thank you, I’ll try !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.