Home > Enterprise >  Can I join several action events of the same type into one in a Java JFrame?
Can I join several action events of the same type into one in a Java JFrame?

Time:11-07

I have a java project in which I am trying to make an effect similar to the hover in CSS, changing the color(icon) every time the mouse passes over a jlabel, but I realized that I am using several methods that do the same thing.

enter image description here

My question is if there is a possibility to unify all of them in a single method or if there is a simpler way to do this kind of animations with a library or something like that.

private void lblPersonalizarMouseExited(java.awt.event.MouseEvent evt) {                                            
    lblPersonalizar.setIcon(icono_personalizari);
}                                           

private void lblPersonalizarMouseEntered(java.awt.event.MouseEvent evt) {                                             
    lblPersonalizar.setIcon(icono_personalizara);
}                                            

private void lblNuevaCompraMouseExited(java.awt.event.MouseEvent evt) {                                           
    lblNuevaCompra.setIcon(icono_comprai);
}                                          

private void lblNuevaCompraMouseEntered(java.awt.event.MouseEvent evt) {                                            
    lblNuevaCompra.setIcon(icono_compraa);
}                                           

private void lblUsuarioMouseEntered(java.awt.event.MouseEvent evt) {                                        
    lblUsuario.setIcon(icono_usuarioa);
}                                       

private void lblUsuarioMouseExited(java.awt.event.MouseEvent evt) {                                       
    lblUsuario.setIcon(icono_usuarioi);
}                                      

private void lblFacturasMouseEntered(java.awt.event.MouseEvent evt) {                                         
    lblFacturas.setIcon(icono_facturasa);
}                                        

private void lblFacturasMouseExited(java.awt.event.MouseEvent evt) {                                        
    lblFacturas.setIcon(icono_facturasi);
}                                       

private void lblMaterialesMouseEntered(java.awt.event.MouseEvent evt) {                                           
    lblMateriales.setIcon(icono_materialesa);
}                                          

private void lblMaterialesMouseExited(java.awt.event.MouseEvent evt) {                                          
    lblMateriales.setIcon(icono_materialesi);
}                                         

private void lblAyudaMouseEntered(java.awt.event.MouseEvent evt) {                                      
    lblAyuda.setIcon(icono_ayudaa);
}                                     

private void lblAyudaMouseExited(java.awt.event.MouseEvent evt) {                                     
    lblAyuda.setIcon(icono_ayudai);
}   
                             

CodePudding user response:

JButton supports image rollover, which would be a much simper solution

Roll over

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new GridBagLayout());
            setBackground(Color.BLACK);

            JButton bagButton = makeButton(new ImageIcon(ImageIO.read(getClass().getResource("/images/Bag.png"))), new ImageIcon(ImageIO.read(getClass().getResource("/images/Bag-Selected.png"))));
            JButton editButton = makeButton(new ImageIcon(ImageIO.read(getClass().getResource("/images/Edit.png"))), new ImageIcon(ImageIO.read(getClass().getResource("/images/Edit-Selected.png"))));
            JButton settingsButton = makeButton(new ImageIcon(ImageIO.read(getClass().getResource("/images/Settings.png"))), new ImageIcon(ImageIO.read(getClass().getResource("/images/Settings-Selected.png"))));

            add(bagButton);
            add(editButton);
            add(settingsButton);

            bagButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Bag was selected");
                }
            });

            editButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Edit was selected");
                }
            });

            settingsButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Settings was selected");
                }
            });
        }

        protected JButton makeButton(Icon icon, Icon rollOverIcon) {
            JButton button = new JButton();
            button.setIcon(icon);
            button.setRolloverIcon(rollOverIcon);
            button.setRolloverEnabled(true);
            button.setBorderPainted(false);
            button.setFocusPainted(false);
            return button;
        }

    }
}

Make sure you take a look at How to Use Buttons, Check Boxes, and Radio Buttons

  • Related