tags:

views:

516

answers:

3

I have two AWT components in a Frame, Panel A and Panel B. I would like panel A to be sized to the height width of the frame (and maintain that size on frame resize), but I would like panel B to overlap A. B will be at a fixed position (0,0 to make it easier) with a fixed height and width. I'm not sure what kind of layout manager I would need to make this work. If I use a null layout, I think I would have to manage the resizing of panel A myself, but it would make the sizing of panel B relatively easy. Any thoughts on how to accomplish this?

thanks, Jeff

A: 

Maybe I'm missing something. If B is fixed size and is at (0,0) and A runs the full width what's the use of having B overlap A? You will never see anything that is placed under B.

I can't think of any of the default layout managers, but Orielly has this one you can use: relative layout manager (see source code link) with documentation. I haven't used it in a long while, but it should beat managing the layout yourself.

CodePartizan
Eventually I will make B not fixed such that it can move to different locations. In the first pass, I'm just keeping it at 0,0. You are correct though, I won't see that piece that B covers, but that is ok. Screen real estate is at a premium and B is small, so I'd rather cover up a small section of A than use something like BorderLayout and waste a large part of the screen I won't use.
Jeff Storey
+2  A: 

Take a look at JLayeredPanes. Here is a tutorial.

edit:

If panelA is an AWT component, you will be hard pressed to get panelB to overlap. From Sun's article entitled Mixing Heavy and Light Components:

Do not mix lightweight (Swing) and heavyweight (AWT) components within a container where the lightweight component is expected to overlap the heavyweight one.

However, if you are looking to have panelA fill the Frame completely, why not add panelB as a component of panelA?

Edit2:

If you can make panelB a heavyweight component, then you can use the JLayeredPane.

Here is a quick mockup that shows how:

public static void main(String[] args){
 new GUITest();
}

public GUITest() {
 frame = new JFrame("test");
 frame.setSize(300,300);
 addStuffToFrame();
 SwingUtilities.invokeLater(new Runnable(){
  public void run() {
   frame.setVisible(true);
  }
 });

}  

private void addStuffToFrame() { 
 Panel awtPanel = new Panel();
 awtPanel.setBackground(Color.blue);
 //here you can fool around with the pane:
 //first, you can see how the layered pane works by switching the 
 //DEFUALT_LAYER and PALLETTE_LAYER back and forth between the two panels
 //and re-compiling to see the results
 awtPanel.setSize(200,300);
 frame.getLayeredPane().add(awtPanel, JLayeredPane.DEFAULT_LAYER);
 //next you comment out the above two lines and 
 //uncomment the following line. this will give you the desired effect of
 //awtPanel filling in the entire frame, even on a resize. 
 //frame.add(awtPanel);

 Panel awtPanel2 = new Panel();
 awtPanel2.setBackground(Color.red);
 awtPanel2.setSize(300,200);
 frame.getLayeredPane().add(awtPanel2,JLayeredPane.PALETTE_LAYER);
}
akf
I should have mentioned that JPanel A is actually an AWT component, my mistake. I'll edit the post.
Jeff Storey
I can make B AWT as well, no problems there. The issue is the PanelA (which I don't have control over) is not a Container, so it doesn't have the add methods. I want to just add them both to some parent container, but that's where I'm not sure about how to lay them out properly so A fills the entire container, but B just fills a portion of it.
Jeff Storey
Jeff, I have updated my answer. Please take a look.
akf
Sorry, got away from this for a little while. I'll check this out in a little while and upvote/accept if it works. Thanks!
Jeff Storey
It didn't really occur to me to use JLayeredPane with AWT components, but this works, thanks.
Jeff Storey
+1  A: 

Your best bet is to have your own LayoutManager. The easiest way is probably to extend or proxy BorderLayout, and have a specific case to layout panel B.

Milan Ramaiya