Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : set a Component on top of another

Tags:

java

swing

jpanel

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?

like image 306
sajad Avatar asked Feb 23 '11 21:02

sajad


People also ask

What is a layered pane in Java?

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.

What is containment hierarchy?

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.

Which pane contains the menu bar and content pane and enables z ordering of other components select one a tabbed pane b layered pane c scroll pane d root pane?

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.

Which method do you use to add a menu bar to a top-level container such as a JFrame?

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.


1 Answers

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);
like image 150
sstendal Avatar answered Nov 15 '22 07:11

sstendal