Moritz Schmidt
Moritz Schmidt

Reputation: 2823

Check Layout of JPanel

I Have the following code piece:

JPanel panel = new JPanel(new GridBagLayout());

I would like to check, if my panel has a GridBagLayout assigned to it. I got it working like this:

if(panel.getLayout().getClass() == GridBagLayout.class) {
   // seems to work
}

Although it works, its kind of hacky since I dont want to use reflection for this.

Is there another way to check the assigned layout?

Upvotes: 2

Views: 273

Answers (1)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

Use instanceof statement Luke

if(panel.getLayout() instanceof GridBagLayout) {
    // seems to work
}

Upvotes: 5

Related Questions