Overlapping Swing Components
I have two AWT components in the frame, panel A and panel B. I would like panel A to be relative to the width of the frame height (and maintain that size when resizing the frame), but I would like panel B to overlap A. B will be in a fixed position (0,0 for simplicity) with a fixed height and width. I'm not sure which layout manager I need to make this work. If I'm using a zero layout I think I'll have to control the resizing of Panel A, but that will make it easier to size Panel B. Any thoughts on how to do this?
thank you jeff
source to share
Take a look at JLayeredPane
s . Here's a tutorial .
edit:
If panelA is an AWT component, it will be difficult for you to press panel B to overlap. From a Sun article entitled Mixing Heavy and Light Components :
Do not mix lightweight (Swing) and heavyweight (AWT) components in a container where the lightweight component is expected to overlap the heavyweight.
However, if you want to completely fill PanelA, why not add panelB as a component of PanelA?
Edit2:
If you can make panel B a heavyweight component, you can use JLayeredPane
.
Here's a quick mockup that shows you 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);
}
source to share
Maybe I missed something. If B is a fixed size and is at (0,0) and A runs through the entire width, what does B use to overlap A? You will never see anything under B.
I can't think of any of the default layout managers, but Orielly has one you can use: a relative layout manager (see source code link) with documentation . I haven't used it in a long time, but it has to manage the layout by itself.
source to share