Search code examples
javaswingjbutton

how to hide background of JButton (which containt icon image)?


I have this code to make a Jbutton with icon image ,and it works.But the problem is that the borders and background of button don't disappear.

I want only the icon image is appear without borders & background of button.

I tried to set setOpaque(false), but all of button disappeared!

What is the wrong in my code ?

_button  = new JButton("Exit");
_button.setHorizontalTextPosition(SwingConstants.CENTER);
_button.setSize(200,130);
//_button.setContentAreaFilled(false);
_button.setBorderPainted(false);
//_button.setOpaque(false);
_button.setIgnoreRepaint(true);
//_button.setFocusable(false);
_button.setIcon(button_icon);
_button.setBounds(200, 200,200, 170);
_button.setRolloverEnabled(true);
_button.setRolloverIcon(button_icon_hover);
_button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//_button.addActionListener(this);
_button.setBackground(null);
_button.setFocusable(true);
_button.setFocusPainted(true);
_button.setForeground(Color.WHITE);
_button.setFont(new Font("Times New Roman",Font.BOLD,35));

Solution

  • Try this

    JButton play = new JButton("This",new ImageIcon("src\play.png"));
            play.setBorderPainted(false); 
            play.setContentAreaFilled(false); 
            play.setFocusPainted(false); 
            play.setOpaque(false);