aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes GUI Problem - JLayeredPane with GridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI Problem - JLayeredPane with GridBagLayout" Watch "GUI Problem - JLayeredPane with GridBagLayout" New topic
Author

GUI Problem - JLayeredPane with GridBagLayout

Ronnie Cohen
Greenhorn

Joined: Mar 08, 2004
Posts: 3
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);
setBackground(Color.lightGray);
gbl = new GridBagLayout();
gbc = new GridBagConstraints();

getContentPane().setLayout(gbl);
JLabel lblName = null;
JFrame f = new JFrame();
JLayeredPane lp = f.getLayeredPane();
JPanel top = new JPanel();
top.setBackground(Color.white);
top.setBounds(50, 70, 150, 200);
JPanel mid1 = new JPanel ();
mid1.setBackground(Color.gray);
mid1.setBounds(60,100,130,40);
JPanel mid2 = new JPanel ();
mid2.setLayout(gbl);
mid2.setBackground(Color.red);
mid2.setBounds(60,150,130,40);
JPanel mid3 = new JPanel ();
mid3.setBackground(Color.gray);
mid3.setBounds(60,200,130,40);
JPanel bottom = new JPanel();
bottom.setBackground(Color.black);
bottom.setBounds(60,120,150,170);
lblName = new JLabel("Test");
lblName.setFont(new Font("Arial", Font.BOLD, 14));
lblName.setForeground(Color.red);
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
f.setSize(600,500);
f.setVisible(true);
}
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.
Thanks...Ronnie
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.
Thanks.


GenRocket - Experts at Building Test Data
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Ronnie Cohen
Greenhorn

Joined: Mar 08, 2004
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GUI Problem - JLayeredPane with GridBagLayout