Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
like image 670
Jason4Ever Avatar asked Dec 03 '11 12:12

Jason4Ever


People also ask

How do I make a background transparent in JButton?

JButton can become transparentIf the value of the opaque property of a JButton is set to false, the background becomes transparent allowing whatever is behind the button to show through. Only the text and the border of the button remain opaque.

How do I remove an icon from a JButton?

Simple, Set the icon to null . It doesn't have an icon, so it removes it. That will work perfectly.

How do I remove the border around text in a JButton?

In Visual Studio with Windows Forms a ToolStrip has TabStop set to false by default. Use my UI fix above to get rid of the dotted borders on Toolbar buttons and ToggleButtons which never have them in Windows with or without focus.

How do I change the image of a JButton in Java?

you can use this code: Icon i=new ImageIcon("image. jpg"); jButton1. setIcon(i);


1 Answers

Try this

JButton play = new JButton("This",new ImageIcon("src\play.png"));
        play.setBorderPainted(false); 
        play.setContentAreaFilled(false); 
        play.setFocusPainted(false); 
        play.setOpaque(false);
like image 151
Yanki Twizzy Avatar answered Oct 13 '22 16:10

Yanki Twizzy