46

Is it possible to make a JButton transparent (including the border) but not the text? I extend swing's JButton and override this:

@Override
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0));
    super.paint(g2);
    g2.dispose();
}

but it makes everything transparent, including the text. Thanks.

Rendicahya
  • 4,205
  • 7
  • 36
  • 56

3 Answers3

129
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    `button.setBorderPainted(false);` may be omit if we want a line around the button – Aerospace Jan 15 '14 at 15:21
  • 2
    *"`setOpaque` saved my life"* Actually @Birdy, it's unnecessary (at least in the five PLAFs I tested on Windows just now). **It is `setContentAreaFilled` that makes a button BG disappear.** See [this code](https://stackoverflow.com/a/10862262/418556) as an example. Sorry for jumping in (checks watch) a bit late on this, but it just came to my attention as a duplicate of a question today. – Andrew Thompson May 03 '19 at 08:51
  • In my case, I also have to add ```button.setFocusPainted(false)``` to completely remove border of my button :) – tunapq Nov 16 '19 at 09:18
13

The following should do the trick.

public class PlainJButton extends JButton {

    public PlainJButton (String text){
        super(text);
        setBorder(null);
        setBorderPainted(false);
        setContentAreaFilled(false);
        setOpaque(false);
    }

    // sample test method
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel pane = new JPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(new PlainJButton("HI!!!!"));
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
    }
}
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
jjnguy
  • 136,852
  • 53
  • 295
  • 323
1

The user of you program can not citified your Button as a button. Use can think it is a Label. So my Solution is half transparent with appearing & disappearing border.

This code is improved, improved codes, better, better performance, more beautiful, pro-fit.

import java.awt.Color;
import static java.awt.Color.blue;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class Test extends JFrame {

    Test() {
        setLayout(null);

        JButton testButton = new JButton("Test_Button");
        testButton.setBounds(200, 200, 200, 20);
        testButton.setOpaque(false); // Must add
        testButton.setContentAreaFilled(false); // No fill
        testButton.setFocusable(false); // I'd like to set focusable false to the button.
        testButton.setBorderPainted(true); // I'd like to enable it.
        testButton.setBorder(null); // Border (No border for now)
        add(testButton);
        
        testButton.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseEntered(MouseEvent e){
                testButton.setBorder(BorderFactory.createLineBorder(blue, 2,true));
                //When enter we can not know our mouse successfully entered to the button. So I'd like to add Border
            }
            @Override
            public void mouseExited(MouseEvent e){
                testButton.setBorder(null);
                //When mouse exits no border.
            }
        });

        // For a half of transparent I'd like a add JPanel behind JButton
        JPanel backPanel = new JPanel();
        backPanel.setBounds(testButton.getBounds()); // Same to buttons bounds.
        backPanel.setBackground(new Color(0, 0, 0, 50)); // Background with transeparent
        add(backPanel);

        JLabel icon = new JLabel();
        icon.setBounds(0, 0, 3840, 2160);
        icon.setIcon(new ImageIcon(getClass().getResource("wallpaper_16105238065ffea49e5ec9d2.04136813.jpeg")));
        add(icon);
        //Background image for test up it.

        testButton.addActionListener((ActionEvent e) -> {
            testButton.setBorder(null); // When mouse clicked border will dissaprear.
            System.out.println("Button clicked!");
            //For check the button works correctly.
        });

        setSize(800, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }
}

My output

DSF.Inc
  • 3,021
  • 2
  • 6
  • 9