0

I want to make JButton with a transparent background icon, which can response to three kind of mode and change its corresponding icon:

  • rollover
  • pressed
  • when become normal mode

I have already set these icons, but when I rollover become black and black by every time, and also when I press the button, the background become very ugly (draw some text or other buttons background etc).

If you know about this problem please tell me what is wrong here. For more details I uploaded some pictures:

enter image description here when normal

enter image description here when pressed or rollover (printed JRadioButton in background)

enter image description here when pressed or rollover (print another JButton's in background)

enter image description here when rollover for many times

enter image description here when pressed or rollover

I just want that the background icon be normal when no rollover or pressed, and become light blue when rollover and become full blue when pressed, I have already set these icons, I am using Intellij IDE GUI form designer.

EDITED Added Code:

public class TransparentTest extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    TransparentTest frame = new TransparentTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TransparentTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        //----------------------------------------------------------
        JLayeredPane layeredPane = new JLayeredPane();
        contentPane.add(layeredPane, BorderLayout.CENTER);

        //----------------------------------------------------------
        JPanel mailJPanel = new JPanel();
        mailJPanel.setBackground(Color.RED);
        mailJPanel.setBounds(0, 0, 422, 243);
        layeredPane.add(mailJPanel);

        //----------------------------------------------------------
        JPanel bottomJPanel = new JPanel();
        //panel_1.setOpaque(false);
        bottomJPanel.setBounds(0, 195, 422, 48);
        layeredPane.setLayer(bottomJPanel, 1);
        layeredPane.add(bottomJPanel);
        // set background
        bottomJPanel.setBackground(Color.BLUE);
        Color color = bottomJPanel.getBackground();
        // set transparency
        bottomJPanel.setBackground(new Color(color.getRed(), color.getGreen(), color.getBlue(), 64));
        // set layout
        FlowLayout fl_bottomJPanel = new FlowLayout();
        fl_bottomJPanel.setHgap(0);
        fl_bottomJPanel.setVgap(0);
        bottomJPanel.setLayout(fl_bottomJPanel);

       //----------------------------------------------------------
       JButton button = new JButton("");
       button.setAlignmentX(Component.CENTER_ALIGNMENT);
       button.setPressedIcon(new ImageIcon(TransparentTest.class.getResource("/start-3.png")));
       button.setRolloverIcon(new ImageIcon(TransparentTest.class.getResource("/start-2.png")));
       button.setHorizontalTextPosition(SwingConstants.CENTER);
       //button.setOpaque(false);
       button.setContentAreaFilled(false);
       button.setBorderPainted(false);
       button.setFocusPainted(false);
       button.setIcon(new ImageIcon(TransparentTest.class.getResource("/start-1.png")));
       // add to bottom panel
       bottomJPanel.add(button);
    }
}
Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
  • 1
    can you show your code – Madhawa Priyashantha Sep 24 '15 at 10:06
  • I have mentioned in the question that I am using Intellij IDE GUI Form Designer, it doesn't generate Java code, It using XML format you know... – Bahramdun Adil Sep 24 '15 at 10:09
  • @BahramdunAdil Still you can post a [mcve] - A compilable example you took some effort in in order to get better help. Please post one that's not made with XML, though. Just pure java code so it's easy for us to copy & paste. – Lukas Rotter Sep 24 '15 at 10:16
  • 3
    You've used `new Color(r, g, b, 0)` or something similar to set the transparent background haven't you, doesn't work does it. Swing components are either opaque or transparent and this is controlled through the use of `setOpaque`. Simply setting the background to a alpha based color doesn't tell Swing that it should also update the are beneath it. First, you need set the component to be transparent, `setOpaque(false)`. Now if you want the component to be translucent, you need to fake, by overriding the components `paintComponent` and filling in the alpha based color – MadProgrammer Sep 24 '15 at 10:45
  • For [example](http://stackoverflow.com/questions/20361432/translucent-jlabel-not-properly-showing-background/20362223#20362223), [example](http://stackoverflow.com/questions/32216625/how-to-make-a-translucent-jpanel-within-the-region-jpanel/32217554#32217554) – MadProgrammer Sep 24 '15 at 10:50

0 Answers0