placing a transparent JPanel on top of another JPanel not working
Asked Answered
H

3

12

I am trying to place a JPanel on top of another JPanel which contains a JTextArea and a button and i want to the upper apnel to be transparent. I have tried it by making the setOpaque(false) of the upper panel. but it is not working. Can anyone help me to get through this? Thanks in advance!

public class JpanelTest extends JPanel
{
    public JpanelTest()
    {
    super();
    onInit();
}
private void onInit()
{
    setLayout(new BorderLayout());

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(new JTextArea(100,100),BorderLayout.CENTER);
    panel.add(new JButton("submit"),BorderLayout.SOUTH);

    JPanel glass = new JPanel();
    glass.setOpaque(false);

    add(panel,BorderLayout.CENTER);
    add(glass,BorderLayout.CENTER);
    setVisible(true);
}

public static void main(String args[])
{
    new JpanelTest();
}
}
Horthy answered 28/12, 2012 at 11:40 Comment(4)
what you do mean by on top? and what exactly is not working?Shoshonean
It means the result shows only the upper panel.Horthy
See the accepted answer at #2670154 Particularly the part about using the BorderLayout...Portfolio
Do you mean translucent?Germinal
P
6

Indeed, it would be useful to tell the reason why you want panels one over another.

Starting with your code, and changing it a lot, I got it to work, but it might not do what you expect...

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame
{
  public Test()
  {
    super();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 200);

    onInit();

    setVisible(true);
  }
  private void onInit()
  {
    JLayeredPane lp = getLayeredPane();

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(new JTextArea(), BorderLayout.CENTER);
    panel.add(new JButton("Submit"), BorderLayout.SOUTH);
    panel.setSize(300, 150); // Size is needed here, as there is no layout in lp

    JPanel glass = new JPanel();
    glass.setOpaque(false); // Set to true to see it
    glass.setBackground(Color.GREEN);
    glass.setSize(300, 150);
    glass.setLocation(10, 10);

    lp.add(panel, Integer.valueOf(1));
    lp.add(glass, Integer.valueOf(2));
  }

  public static void main(String args[])
  {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        new Test();
      }
    });
  }
}

If totally transparent, well, it is like it isn't here! When opaque, it just covers some of the GUI, but doesn't prevent mouse clicks, for example.

Portfolio answered 28/12, 2012 at 12:32 Comment(0)
A
5

1) there are a few ways, there no issue to put JPanel, with covering full JFrames/JPanel area or only part of Rectangle / Dimension that returns JFrames/JPanel

  • use JLayer(Java7) based on JXLayer (Java6)

  • use GlassPane

  • use JViewport

  • use OverlayLayout

  • use transucent JDialog / JWindow

2) everything depends of if you want to protect against mouse and key events from the top layer to bottom, or not (to avoiding redispatch events from - to and vice versa)

Alate answered 28/12, 2012 at 12:11 Comment(0)
R
4

Check out this tutorial on using Swing Root Panes.

The glass pane is useful when you want to be able to catch events or paint over an area that already contains one or more components. For example, you can deactivate mouse events for a multi-component region by having the glass pane intercept the events. Or you can display an image over multiple components using the glass pane.

Regelation answered 28/12, 2012 at 11:46 Comment(1)
@Nikhil: I agree this answer sounds like what you want, though you have not really told us what your requirements (or problems) are.Bobbibobbie

© 2022 - 2024 — McMap. All rights reserved.