• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI Problem - JLayeredPane with GridBagLayout  RSS feed

Ronnie Cohen
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to Java and am working on an GUI. I created a screen with JLayeredPane and want to populate the JPanes with JCombo, and JLabel components.
I want the group of JPanes/JLabels ect. to be in the center and stay there when the screen is enlarged. I tried to use the GrideBagLayout with the JlayeredPane, but it would not work.
Here is an example of what I want the screen to look like:
import javax.swing.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class SimpleLayers extends JFrame{
private GridBagLayout gbl;
private GridBagConstraints gbc;
public SimpleLayers (String caption) {
super (caption);
gbl = new GridBagLayout();
gbc = new GridBagConstraints();

JLabel lblName = null;
JFrame f = new JFrame();
JLayeredPane lp = f.getLayeredPane();
JPanel top = new JPanel();
top.setBounds(50, 70, 150, 200);
JPanel mid1 = new JPanel ();
JPanel mid2 = new JPanel ();
JPanel mid3 = new JPanel ();
JPanel bottom = new JPanel();
lblName = new JLabel("Test");
lblName.setFont(new Font("Arial", Font.BOLD, 14));
mid1.add(lblName, gbc);
set_gbc(lblName, 0,0,5,1);
//Place the buttons in different layers
lp.add(bottom, new Integer(1));
lp.add(top, new Integer(2));
lp.add(mid1, new Integer(3));
lp.add(mid2, new Integer(4));
lp.add(mid3, new Integer(5));
// Show the Frame
public static void main(String[] args)
new SimpleLayers("Java Test");
private void set_gbc(Component c, int row, int column, int width, int height) {
gbc.gridy = row;
gbc.gridx = column;
gbc.gridwidth = width;
gbc.gridheight = height;
Would someone please help me understand what is wrong. That is How to offset the shadow, have the large pane and three other planes with their components offset the shadow and increse correctly when the screen is enlarged.
Gregg Bolinger
Ranch Hand
Posts: 15304
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ronnie Ronnie,
Welcome to Javaranch! We don't have too many rules around here but we do have a Naming Policy. Please adjust your display name accordingly.
You can change your display name here.
Nathan Pruett
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is the layered pane... if you want components to overlap each other, you aren't going to be able to use a regular layout manager. You're just going to have to do a setBounds() manually on every component. (Another problem is that you set the layout onto the SimpleLayers JFrame's content pane you are in the process of creating, and then you create another JFrame in the constructor and actually add the components to that one.) Check out the Swing tutorial section on How to Use Layered Panes, especially the section on "Laying Out Components in a Layered Pane".

You could add a componentListener to the parent frame and re-position all the components to be centered whenever the parent frame changes size, using the normal setBounds().

I'm not sure why you are using a layered pane for your GUI, but one option may be to use a JDesktopPane with a JInternalFrame for each of your panels. Even if you want the same look as you currently have, you shouldn't need to add each of the "buttons" to the layered pane, just the "top" panel (unless you plan on dragging the buttons around independently of their surrounding panel). You should be able to set a layout on the "top" panel and add all the buttons to it. You could simulate the "bottom" panel with a 3D looking border on the "top" panel.
Ronnie Cohen
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I will try your suggestions. The reason for the strange layout is because I am trying to mimic and existing VB6 program. The program uses "tiles" to seperate input groups.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!