Josh Argent
Josh Argent

Reputation: 82

Adding components dynamically in java

I have a JPanel which takes up the majority of the screen space, it's called contentPane. Inside this is another JPanel called canvas. I want to add another JPanel to contentPane during runtime which will act as a kind of floating dialog, so I need it to be on the very top layer some how.

The event to add this dialog is originally fired from a MouseDown event in canvas. My code so far doesn't seem to work. Nothing appears at all:

JPanel editor = new JPanel();
editor.setLocation(500, 100);
editor.setMaximumSize(new Dimension(100, 100));
app.contentPane.add(editor);
app.validate();

Upvotes: 2

Views: 121

Answers (1)

camickr
camickr

Reputation: 324118

so I need it to be on the very top layer some how.

You can't just add components on top of one another. Swing layouts work in 2 dimensions.

See How to Decorate Components with the JLayer Class or How to Use Layered Panes or maybe a Glass Pane.

Upvotes: 2

Related Questions