I am writing a program in java. I have a main JPanel that has two JPanel and one Canvas added on it. I aim to resize the Canvas while running the program. When I maximized the Canvas i want it to be always on top of the other component.
How can I set this property for my Canvas?
A layered pane is a Swing container that provides a third dimension for positioning components: depth, also known as Z order. When adding a component to a layered pane, you specify its depth as an integer. The higher the number, closer the component is to the "top" position within the container.
A containment hierarchy is a tree of components that has a top-level container as its root. We'll show you one in a bit. Each GUI component can be contained only once.
The Z ordering that the layered pane provides enables behavior such as displaying popup menus above other components. The root pane adds the menu bar and content pane to its layered pane at this depth. If you don't specify a component's depth when adding it to a layered pane, the layered pane puts it at this depth.
Question 2: Which method do you use to add a menu bar to a top-level container such as a JFrame ? Question 3: Which method do you use to specify the default button for a top-level container such as a JFrame or JDialog ? Answer 3: JRootPane 's setDefaultButton method.
You could replace your main JPanel with a JLayeredPanel. A layered panel will let you specify that some child components should be layered above other child components.
I.e.:
JLayeredPane pane = new JLayeredPane();
JLabel ontop = new JLabel("On top");
JLabel behind = new JLabel("Behind");
pane.add(ontop, 2, 0);
pane.add(behind, 1, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With