Check Layout of JPanel

Moritz Schmidt :

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?

Sergiy Medvynskyy :

Use instanceof statement Luke

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25245&siteId=1