GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes reduce gap between image and button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "reduce gap between image and button" Watch "reduce gap between image and button" New topic
Author

reduce gap between image and button

swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
Hi

I have displayed a button with a image on it.
there is gap between actual image and button
I have to reduce the gap between the image and the button.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
Don't know. You would have to provide more details, about how you are locating the Components, layout manager used, etc.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

How have you added the icon to the button? Using the button's setIcon method? If so, check out the API of JButton's parent, AbstractButton. Look at the setXXX methods, there is one you want to look at.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
in my app, there is a image which is of round shape. When i display that image on the button I want remaining area of the button should be transparent. is it possible
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
I have done,

JButton longButton = new JButton(new ImageIcon("c:/normal.png"));
longButton.setPressedIcon(new ImageIcon("c:/normalpress.png"));
longButton.setOpaque(false);
longButton.setBorderPainted(false);
longButton.setFocusPainted(false);
longButton.setBorder(new EmptyBorder(0,0,0,0));
longButton.setBackground(Color.blue);

following code, still I get the pressed look after pressing the button.

I dont want that effect which came after pressing the button (button is in pressed state)
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
I dont want that effect which came after pressing the button (button is in pressed state)


That is not what your first question asked. We are not mind readers.

The pressing effect is caused by the Border, so get rid of the Border on the button.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
swapnel surade wrote:I have done,

JButton longButton = new JButton(new ImageIcon("c:/normal.png"));
longButton.setPressedIcon(new ImageIcon("c:/normalpress.png"));
longButton.setOpaque(false);
longButton.setBorderPainted(false);
longButton.setFocusPainted(false);
longButton.setBorder(new EmptyBorder(0,0,0,0));
longButton.setBackground(Color.blue);

following code, still I get the pressed look after pressing the button.

I dont want that effect which came after pressing the button (button is in pressed state)


First of all, pressing the button will cause the icon to change from normal.png to normalpress.png. If you don't want pressing the button to do that, then you shouldn't be calling setPressedIcon() [or you should set the pressed icon to the same as unpressed]. So I presume that's not what you are asking.

If normal.png and normalpress.png are the same size and have no transparent pixels, then I don't understand how you would see any "effect which came after pressing the button" since you have gone to the unusual step of replacing the button's border.
If they are different sizes or normalpress.png has transparent pixels, then you will probably (depending on your LnF) see the background color change. You should be able to disable this by calling longButton.setContentAreaFilled(false).


bitguru blog
 
GeeCON Prague 2014
 
subject: reduce gap between image and button