Effect on edge of a JButton component

4

How do I create a smooth (fade) transition from the border of a JButton when hovering the mouse?

My code looks like this:

@Override
public void mouseEntered(MouseEvent e) {
    Object temp = e.getSource();
    if (temp instanceof JButton)
        ((JButton)temp).setBorder(new LineBorder(Color.WHITE, 1, false);
    /* restante do código */
}

@Override
public void mouseExited(MouseEvent e) {
    Object temp = e.getSource();
    if (temp instanceof JButton)
       ((JButton)temp).setBorder(null);
    /* restante do código */
}

How do I get this kind of result? I looked in some forums and articles, but nothing that would address this detail.

    
asked by anonymous 26.04.2014 / 01:27

1 answer

1

I think you're looking for something like this:

packagebr;importjava.awt.AlphaComposite;importjava.awt.Color;importjava.awt.FlowLayout;importjavax.swing.*;importjavax.swing.JButton;importjavax.swing.JFrame;publicclassCriarJButtonextendsJFrame{privatestaticfinallongserialVersionUID=1L;publicCriarJButton(){this.getContentPane().setLayout(newFlowLayout());finalbotaoFadebt=newbotaoFade("StackOverflow.pt");
        bt.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
               bt.setAlpha((float)0.6);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                bt.setAlpha((float)1);
            }
        });
        add(bt);

    }

    private static void cria_MostraInterface() {

        JFrame painel = new CriarJButton();
        painel.pack();
        painel.setVisible(true);
        painel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                cria_MostraInterface();
            }
        });
    }

    private static class botaoFade extends JButton {

        private static final JButton lafDeterminer = new JButton();
        private static final long serialVersionUID = 1L;
        private boolean rectangularLAF;
        private float alpha = 1f;

        botaoFade() {
            this(null, null);
        }

        botaoFade(String texto) {
            this(texto, null);
        }

        botaoFade(String texto, Icon icon) {
            super(texto, icon);
            setOpaque(false);
            setFocusPainted(false);
        }

        public float getAlpha() {
            return alpha;
        }

        public void setAlpha(float alpha) {
            this.alpha = alpha;
            repaint();
        }

        @Override
        public void paintComponent(java.awt.Graphics g) {
            java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            if (rectangularLAF && isBackgroundSet()) {
                Color c = getBackground();
                g2.setColor(c);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            super.paintComponent(g2);
        }

        @Override
        public void updateUI() {
            super.updateUI();
            lafDeterminer.updateUI();
            rectangularLAF = lafDeterminer.isOpaque();
        }
    }
}

I have chosen to modify the whole button, but if you want to apply only on the border, it will not be that difficult.

If you want, you can download the code at gist .

    
07.07.2014 / 20:33