JLayeredPane/GridBagLayout


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: JLayeredPane/GridBagLayout

  1. #1
    Join Date
    Sep 2003
    Posts
    36

    JLayeredPane/GridBagLayout

    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();

    // setLayout(gbl);

    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);

    // mid1.add(lblName);

    //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

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    i could fake the whole thing without using a single layered pane or panel. for more info, read the gridbag tutorial in my sig

    if the images dont show, my webhost is being lame, wait a while and try again
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Sep 2003
    Posts
    36
    Hi JCard

    Using a GridbagLayout for the screen is a great idea. I think I never thought of that approach because it seems hard.

    Hopefully I will be able to make it work.

    Thanks.

    Ronnie

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    if you get stuck, post to the gridbag thread (on a different board - registration required)
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Sep 2003
    Posts
    36

    Thanks

    Is the fourm the same one where I found your tutroial?

    Thanks...(VB was a breeze...)

    Ronnie

  6. #6
    Join Date
    Sep 2003
    Posts
    36

    Thanks

    Thanks Steve. I think I need the Array List becuase I am not certain of the number of variables in my array.

    Ronnie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center