Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm looking for some solution to turn an opaque, null JFrame background to transparent (no Translucent) but showing swing components like buttons containers etc... here a example.

Default JFrame

http://i.picasion.com/resize80/5c005816e8bd52d0d3325551ef14b1b1.jpg

Something like, HERE disable the background

http://i.picasion.com/resize80/742f5a46663852643b8aef69436e6dba.jpg

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
110 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is a simple two step process...

First, you need to make a transparent frame...

JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

For every component you want to be see through, you need to make sure that it's set to be transparent, for example...

public class TestPane extends JPanel {

    public TestPane() {
        setOpaque(false);
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(4, 4, 4, 4);
        add(new JLabel("Product"), gbc);
        gbc.gridy++;
        add(new JLabel("Company"), gbc);
        gbc.gridy++;
        add(new JLabel("Phone"), gbc);

        gbc.gridx++;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(new JTextField(10), gbc);
        gbc.gridy++;
        add(new JComboBox(), gbc);
        gbc.gridy++;
        add(new JTextField(10), gbc);

        JPanel buttons = new JPanel();
        buttons.setOpaque(false);
        buttons.add(new JButton("Add"));
        buttons.add(new JButton("Update"));
        buttons.add(new JButton("Delete"));
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        add(buttons, gbc);

        gbc.gridy++;
        JPanel filler = new JPanel();
        filler.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
        add(filler, gbc);

        gbc.gridy++;
        gbc.fill = GridBagConstraints.BOTH;
        add(new JScrollPane(new JTable(new DefaultTableModel(new String[]{"Product", "Company", "Phone"}, 0))), gbc);

    }

}

TransparentFrame

And a runnable example...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setOpaque(false);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            add(new JLabel("Product"), gbc);
            gbc.gridy++;
            add(new JLabel("Company"), gbc);
            gbc.gridy++;
            add(new JLabel("Phone"), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JComboBox(), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);

            JPanel buttons = new JPanel();
            buttons.setOpaque(false);
            buttons.add(new JButton("Add"));
            buttons.add(new JButton("Update"));
            buttons.add(new JButton("Delete"));
            gbc.gridx = 0;
            gbc.gridy++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(buttons, gbc);

            gbc.gridy++;
            JPanel filler = new JPanel();
            filler.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
            add(filler, gbc);

            gbc.gridy++;
            gbc.fill = GridBagConstraints.BOTH;
            add(new JScrollPane(new JTable(new DefaultTableModel(new String[]{"Product", "Company", "Phone"}, 0))), gbc);

        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

    }

}

This is also a great way take "fake" a translucent frame...

Translucent Frame

public class TestPane extends JPanel {

    //...

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(getBackground());
        g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.dispose();
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...