tags:

views:

3698

answers:

4

Hi everyone,

Well, I have an image that I would like to put as a background to a button (or something clicable). The problem is that this image is round, so I needed to show this image, without any borders and etc.

The JComponent that holds this button have a custom background, so the button really need to only show the image.

After searching google, I couldnt manage to do so, I have tryed all the following but with no luck:

button.setBorderPainted(false);
button.setContentAreaFilled(false); 
button.setOpaque(true);

And after I paint the icon at the background, the button paints it, but holds an ugly gray background with borders and etc. I have also tried to use a JLabel and a JButton. And to paint an ImageIcon at it, but if the user resizes or minimizes the window, the icons disappear!

Please, does anyone have a clue on this? I just need to paint and round image to a JComponent and listen for clicks at it..

A: 

Opacity should be set to false, so

button.setOpaque(false);

could already be what you want.

Bombe
+2  A: 

Did you try the following ?

button.setOpaque(false);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); // Especially important

setBorder(null) might work, but there is a bug sun explaining it is by design that the UI set a border on a component unless the client sets a non-null border which does not implement the UIResource interface.

Rather than the JDK itself setting the border to an EmptyBorder when null is passed in, the clients should set an EmptyBorder themselves (a very easy workaround). That way there is no confusion about who's doing what in the code.

VonC
A: 

I would recommend overriding paint(Graphics g) method as so:

class JImageButton extends JComponent implements MouseListener {
    private BufferedImage img = null;

    public JImageButton(BufferedImage img) {
        this.img = img;
        setMinimumSize(new Dimension(img.getWidth(), img.getHeight()));
        setOpaque(false);
        addMouseListener(this);
    }

    public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }
}
pek
+1  A: 

create a button

            JButton addBtn=new JButton("+");
    addBtn.setBounds(x_pos,y_pos,30,25);
    addBtn.setBorder(new RoundedBorder(10));//10 is the radius 
    addBtn.setForeground(Color.BLUE);

while setting the border for a JButton call the overridden javax.swing.border.Border class

addBtn.setBorder(new RoundedBorder(10));

here is the class

private static class RoundedBorder implements Border {

        private int radius;

        RoundedBorder(int radius) {
            this.radius = radius;
        }
        public Insets getBorderInsets(Component c) {
            return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
        }


        public boolean isBorderOpaque() {
            return true;
        }


        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            g.drawRoundRect(x,y,width-1,height-1,radius,radius);
        }
    }
Lalchand