Saturday, April 24, 2010

Switching components in Swing with MigLayout

During Swing application development, I had a situation when at one location I had to display one component or another, based on third component value.

Normally, I would have to create some tricks with sizes or remove items from layout, but with MigLayout this is fantastically easy.

There is magic parameter "hidemode". If you set this value to 2 it will automatically shrink element's sizes to 0 when they are hidden.

So complete solution is to put all switchable parameters to same cell and set this "hidemode" to 2.

panel.add(inputField, "split 2,hidemode 2");
panel.add(comboField, "hidemode 2");

Now, when you need to show one element, you just hide other and vice versa.

inputField.setVisible(true);
comboField.setVisible(false);

No comments:

Post a Comment