Check Layout of JPanel

Viewed 127

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?

1 Answers

Use instanceof statement Luke

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