Seba Paz
Seba Paz

Reputation: 55

"Extends JFrame" and JPanel?

The following code, as you see below:

public class app extends javax.swing.JFrame implements Runnable {

extends JFrame. But I also need it to extend JPanel, in order to make a transparent JPanel. The problem is that I can't extend both, java throws a mistake:

If I extend JPanel I'm able to make a transparent JPanel, but the program can't run because there's a mistake in a few lines of code (mistake that disappears if I extend JFrame).

However, if I extend JFrame the program will run just fine, but it keeps me away from doing a transparent JPanel. How can I solve this?

Upvotes: 1

Views: 3043

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347334

Basically, create your custom class extending from a JPanel, use setOpaque to false to make it transparent.

Create an instance of JFrame, set it to undecorated and adjust it's opacity separately.

Add the custom panel to the frame...

Example

enter image description hereenter image description here

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TransparentPanel {

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

    public TransparentPanel() {
        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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                BufferedImage img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/issue459.jpg"));
                final JLabel label = new JLabel(new ImageIcon(img.getScaledInstance(-1, 200, Image.SCALE_SMOOTH)));
                label.setLayout(new CardLayout());

                JPanel menu = new JPanel(new GridBagLayout());
                JButton button = new JButton("Show");
                menu.add(button);

                JPanel transparent = new JPanel(new GridBagLayout());
                transparent.setOpaque(false);
                transparent.add(new JLabel("Look, I'm see through"));

                label.add(menu, "menu");
                label.add(transparent, "transparent");
                CardLayout layout = (CardLayout) label.getLayout();
                layout.show(label, "menu");

                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        CardLayout layout = (CardLayout) label.getLayout();
                        layout.show(label, "transparent");
                    }
                });

                add(label);
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }
    }    
}

Upvotes: 3

Josh M
Josh M

Reputation: 11947

A class can only ever extend one other class, that's why it won't allow you to extend more than 1 class. here is an explanation why that is the case. Perhaps you should try reading this for more information about such window modifications (window transparency and shapes)

If you want specifically a transparent JPanel, perhaps you should look at this answer as it could explain it better than I could.

Upvotes: 1

Related Questions