Confused with OverlayLayout class
Hi All,
I am BSc(IT) student and currently learning advanced java. I am expecting you help to solve my confusion. I want to be aware about the 'OverlayLayout class' of the java programming. I don't know how to use the properties of this java class. Do you have knowledge about the 'OverlayLayout class' of java? If yes then please try to share it. Please also provide proper example this class.
Re: Confused with OverlayLayout class
Hi friend,
A OverlayLayout class is used to arrange components over the top of each other. The requested size of the container will be the largest requested size of the children, taking alignment needs into consideration. The alignment is based upon what is needed to properly fit the children in the allocation area. The children will be placed such that their alignment points are all on top of each other. This class uses the java package known as 'java.lang.Object'.
Re: Confused with OverlayLayout class
Hi,
I recommend you to go through some following methods of OverlayLayout class:
- removeLayoutComponent(Component cmp)
- preferredLayoutSize(Container tgt)
- maximumLayoutSize(Container tgt)
- addLayoutComponent(String nm, Component cmp)
- addLayoutComponent(Component cmp, Object cstrn)
Re: Confused with OverlayLayout class
Example of OverlayLayout class:
Code:
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
public class OverlayDemo {
public static void main(String args[]) {
JFrame frameQ = new JFrame("Overlay Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public boolean isOptimizedDrawingEnabled() {
return false;
}
};
LayoutManager overlayQ = new OverlayLayout(panel);
panel.setLayout(overlayQ);
JButton buttonQ = new JButton("Small");
buttonQ.setMaximumSize(new Dimension(25, 25));
buttonQ.setBackground(Color.white);
buttonQ.setAlignmentX(0.5f);
buttonQ.setAlignmentY(0.5f);
panel.add(buttonQ);
buttonQ = new JButton("Large");
buttonQ.setMaximumSize(new Dimension(100, 100));
buttonQ.setBackground(Color.black);
buttonQ.setAlignmentX(0.5f);
buttonQ.setAlignmentY(0.5f);
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(200, 300);
frame.setVisible(true);
}
}
Re: Confused with OverlayLayout class
Hello friend,
The OverlayLayout() method of OverlayLayout class is used for the construction of a layout manager. This is very essential for to perform the children class overlay arrangement of the. To do this you just need to dedicate the layout manager to the particular container. The valid parameter for OverlayLayout() method is 'target'. The 'target' is nothing but the name of the specified container over which you want to apply this method.
Re: Confused with OverlayLayout class
Please refer bewlo java code, which will demonstrate you the use of OverlayLayout class as well as it's methods and constructors:
Code:
import javax.swing.OverlayLayout;
public class ClSample {
public static void main(String args[]) {
JFrame frameW = new JFrame("Overlay Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public boolean isOptimizedDrawingEnabled() {
return false;
} };
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
JButton button = new JButton("Small");
buttonW.setMaximumSize(new Dimension(25, 25));
buttonW.setBackground(Color.white);
panel.add(button);
panel.add(button);
framew.add(panel, BorderLayout.CENTER);
framew.setSize(300, 300);
framew.setVisible(true);
}
}