Change the color of the components while they have the pointer on them

1

Most swing components change color when they have the pointer on them. However, I would like to personalize that color (they are always painted blue), I have searched for information but I have not found anything. I am particularly interested in knowing how to do it with the JButton, with the elements of a JMenuBar, JMenuItem and the elements of a jList.

    
asked by Abner 19.09.2017 в 09:10
source

1 answer

2

Good, if you want to change the color of the button or its style when you mouse over it you should use the methods mouseEntered (MouseEvent event) and mouseExited (mouseEvent) that bring the JComponents .

An example: The background color of the JButton component is changed

JButton buscarButton = new JButton("Buscar");
        buscarButton.setBackground(Color.WHITE);
        buscarButton.setBorderPainted(false);
        buscarButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent arg0) {
                buscarButton.setBackground((Color.BLUE));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                buscarButton.setBackground((Color.WHITE));
            }
        });
        buscarButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                buscar();
            }
        });
    
answered by 19.09.2017 в 10:13