0

I have a list of buttons inside a JLabel. The buttons are images and the background of that image is transparent, however the button iself is bigger than the image and it covers the background image, here's what I mean:

http://i.imgur.com/uSBouqO.png

This is the code I have:

JPanel buttons = new JPanel(new GridLayout(0, 1));
        JButton butoMapa = null;
        try {
            butoMapa = new JButton(new ImageIcon(ImageIO.read(new File("imatges/Mapa.png"))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        butoMapa.setOpaque(false);
        butoMapa.setContentAreaFilled(false);
        butoMapa.setBorderPainted(false);
        butoMapa.addActionListener(this);

        JButton butoEtnies = null;
        try {
            butoEtnies = new JButton(new ImageIcon(ImageIO.read(new File("imatges/Etnies.png"))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        butoEtnies.addActionListener(this);

        JButton butoComandes = null;
        try {
            butoComandes = new JButton(new ImageIcon(ImageIO.read(new File("imatges/Comandes.png"))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        butoComandes.addActionListener(this);

        JButton butoSurtir = null;
        try {
            butoSurtir = new JButton(new ImageIcon(ImageIO.read(new File("imatges/Surtir.png"))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        butoSurtir.addActionListener(this);

        SomePanel label2 = new SomePanel();
        label2.add(buttons);

        frame.add(label2, BorderLayout.EAST);
        buttons.add(butoMapa);
        buttons.add(butoEtnies);
        buttons.add(butoComandes);
        buttons.add(butoSurtir);

        //JPanel right = new JPanel(new BorderLayout());
       // right
        //right.add(buttons, BorderLayout.NORTH);


        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

SomePanel code:

class SomePanel extends JPanel {
    private BufferedImage image;

    public SomePanel() {
        try {
            image = ImageIO.read(getClass().getResource("imatges/costat.png"));
        } catch (IOException ex) {}
        //add(new JButton("Hello"));
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }
}

Note that I tested those commands with the first map but it still doesn't show the background of the label on the right. What am I missing?

camickr
  • 321,443
  • 19
  • 166
  • 288
p. bosch
  • 139
  • 2
  • 10

1 Answers1

3

The button has a default Border. If you don't want the border you can use:

button.setBorderPainted( false );

You may also want to use:

button.setFocusPainted( false );
button.setContentAreaPainted( false );

Edit:

Oops, I just noticed you do use the above code for your first button (you of course will need to repeat if for your other buttons).

I would guess the problem is with the buttons panel. You also need to make the panel transparent:

JPanel buttons = new JPanel(new GridLayout(0, 1));
buttons.setOpaque( false );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Oh my god you just made it! I've been more than 3 hours looking for something like this and all I have to do was make the PANEL transparent... Thank you SO MUCH !! – p. bosch May 04 '14 at 16:08