acampb311
acampb311

Reputation: 333

Adding two JPanels to a JLayeredPane

I am trying to add a JPanel to a JLayeredPane with only partial success. I am able to see both of the panels separately, but not when I add them together. When I put them both together, only the DEFAULT_LAYER shows. Any thoughts would be great.

import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import javax.swing.JLayeredPane;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Container;
import java.awt.Component;
import java.awt.Insets;
import java.util.ArrayList;
import java.awt.LayoutManager;

public class ImageEditorView 
{
   public static int dimensionWidth  = 1280;
   public static int dimensionHeight = 640;
   private Color currentColor = Color.WHITE;
   private JLayeredPane layeredPane;

   private JPanel createEditPanel()
   {
      final CirclePanel editPanel = new CirclePanel();
      //editPanel.setLayout(new CircleLayout(true));

      editPanel.setBounds(50, 0, 150, 150);
      editPanel.setOpaque(true);

      return editPanel;
   }

   private JPanel createImageView()
   {
      JPanel imageView = new JPanel(new GridBagLayout());
      imageView.setPreferredSize(new Dimension(dimensionWidth, dimensionHeight));
      imageView.setBounds(0, 0, dimensionWidth, dimensionHeight);
      imageView.setBackground(currentColor);
      return imageView;
   }

   private void addToLayeredPane(JComponent component, int level)
   {
      //This prints out what is expected. values of 1 and 100 respectfully
      System.out.println("The level has a value of " + level);
      layeredPane.add(component, level);
   }

   public void createAndShowGUI()
   {
      JFrame f = new JFrame("ImageEditor");
      f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      f.setLocationByPlatform(true);

      layeredPane = new JLayeredPane();
      //Both createImageView and createEditPanel return with the type JPanel
      addToLayeredPane(createImageView(), JLayeredPane.DEFAULT_LAYER);
      addToLayeredPane(createEditPanel(), JLayeredPane.PALETTE_LAYER);

      f.setSize(dimensionWidth, dimensionHeight);
      f.setContentPane(layeredPane);
      f.setMinimumSize(f.getSize());
      f.setVisible(true);
   }
}

class CirclePanel extends JPanel
{
   @Override
   protected void paintComponent(Graphics g)
   {
      g.drawOval(0, 0, g.getClipBounds().width, g.getClipBounds().height);
   }
}

Upvotes: 0

Views: 669

Answers (1)

camickr
camickr

Reputation: 324088

A JPanel is opaque by default. So you will only see the panel that has been added to the top layer. Try:

panel.setOpaque(false);

on the top panel.

If you need more help then post a proper SSCCE that demonstrate the problem. The code you posted does not compile and therefore is not executable.

Edit:

The problem is your addToLayeredPane(...) method. You are using a layer parameter of "int". It needs to be Integer.

Since you are adding the component using an int value the component is added to the panel normally and is painted in its ZOrder, which basically means the last component added is painted first so your "imageview" is painted on top of the "editview".

Also, you still are not using setOpaque(false) on the "editpanel" as I suggested. The code will only appear to work because you are NOT doing custom painting properly. You should always invoke super.paintComponent(g) at the start of your painting method. This will automatically paint the background which is why you need to make the panel transparent.

Upvotes: 2

Related Questions