atarw / material-ui-swing

A modern, Material Design UI for Java Swing
MIT License
653 stars 86 forks source link

Background-color of a button is turned to default after hovering #90

Closed MJ-DEV91 closed 4 years ago

MJ-DEV91 commented 4 years ago

Hi. Please consider below button:

JButton jButton1 = new javax.swing.JButton();
jButton1.setBackground(MaterialColors.LIGHT_BLUE_400);

The background color is going to be blue and at first, everything is OK: https://pasteboard.co/IENpssv.jpg

When I hover it, it's turned to gray: https://pasteboard.co/IENpTxo.jpg

But when I move the cursor, it is not turned to blue: https://pasteboard.co/IENqCrx.jpg

The source code is as below:

import mdlaf.MaterialLookAndFeel;
import mdlaf.animation.MaterialUIMovement;
import mdlaf.themes.MaterialLiteTheme;
import mdlaf.utils.MaterialColors;

import javax.swing.*;

public class NewJDialog extends javax.swing.JDialog {

    /**
     * Creates new form NewJDialog
     */
    public NewJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        JButton jButton1 = new javax.swing.JButton();
        jButton1.setBackground(MaterialColors.LIGHT_BLUE_400);
        jButton1.setText("Test Button");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(166, 166, 166)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(136, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(191, 191, 191)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(184, Short.MAX_VALUE))
        );

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            MaterialLookAndFeel material = new MaterialLookAndFeel(new MaterialLiteTheme());
            UIManager.setLookAndFeel(material);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewJDialog dialog = new NewJDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
}
vincenzopalazzo commented 4 years ago

Hi @MJ-DEV91,

So the event mouse hover is a simple MouseEvent and it gets the color to the bootstrap theme,

This is default code for add the mouse event

if (UIManager.getBoolean("Button.mouseHoverEnable")) {
            JButton b = (JButton) button;
            if (!b.isDefaultButton()) {
                button.addMouseListener(MaterialUIMovement.getMovement(button, UIManager.getColor("Button.mouseHoverColor")));
            }
        }

Where the UIManager.getColor("Button.mouseHoverColor") is MaterialColors.GRAY_400.

For actual implementation of the button for work well, you must be reset the listener when set the new beckgroud, an example with the your code

jButton1.setBackground(MaterialColors.LIGHT_BLUE_400);
jButton1.addMouseListener(MaterialUIMovement.getMovement(jButton1, MaterialColors.GRAY_500));

You can fix it with this code

package integration.gui.mock;

import mdlaf.MaterialLookAndFeel;
import mdlaf.animation.MaterialUIMovement;
import mdlaf.themes.MaterialLiteTheme;
import mdlaf.utils.MaterialColors;

import javax.swing.*;

/**
 * @author https://github.com/vincenzopalazzo
 */
public class Issue87ColorMouseHover extends JDialog {

        /**
         * Creates new form NewJDialog
         */
        public Issue87ColorMouseHover(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }

        private void initComponents() {

            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

            JButton jButton1 = new javax.swing.JButton();
            jButton1.setBackground(MaterialColors.LIGHT_BLUE_400);
            jButton1.setForeground(MaterialColors.COSMO_LIGTH_GRAY);
            jButton1.addMouseListener(MaterialUIMovement.getMovement(jButton1, MaterialColors.GRAY_500));
            jButton1.setText("Test Button");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                    .addGap(166, 166, 166)
                                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap(136, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                    .addGap(191, 191, 191)
                                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap(184, Short.MAX_VALUE))
            );

            pack();
        }

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            // TODO add your handling code here:
        }

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            try {
                MaterialLookAndFeel material = new MaterialLookAndFeel(new MaterialLiteTheme());
                UIManager.setLookAndFeel(material);
            } catch (Exception ex) {
                java.util.logging.Logger.getLogger(Issue87ColorMouseHover.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }

            /* Create and display the dialog */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    Issue87ColorMouseHover dialog = new Issue87ColorMouseHover(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                }
            });
        }
}

I do not have a good idea for change the color inside the listener and I don't want to create a new listener when is call the method paint because the method is called more and more times.

More important I'm a little engaged, but I have scheduled a representation of the JButton style, look this milestone. I think this milestone forces to reprojecting the MouseHover listener, I hope to add the better method for not forced the recreated listeners to manual. But I have need a little time, I am poor open source developer :)

MJ-DEV91 commented 4 years ago

Thanks, you're doing a great job