13

I am starting out using MiGLayout for my GUI design, and one of the feature I would need is to show/hide certain components based on the state of other components.

I was going to code this myself, when I noticed that one of the Component Constraints supported by MiGLayout is hidemode.

Does this do what I think it does? If so, how do I trigger a hide / unhide action, assuming the hidemode has been set? (After looking through the documentation I was able to find out how to set the hidemode, but not how it is used thereafter)


hidemode

Sets the hide mode for the component. If the hide mode has been specified in the This hide mode can be overridden by the component constraint. The hide mode specified how the layout manager should handle a component that isn't visible. The modes are:

0 - Default. Means that invisible components will be handled exactly as if they were visible.
1 - The size of the component (if invisible) will be set to 0, 0.
2 - The size of the component (if invisible) will be set to 0, 0 and the gaps will also be set to 0 around it.
3 - Invisible components will not participate in the layout at all and it will for instance not take up a grid cell.
Example: "hidemode 1"

Community
  • 1
  • 1
bguiz
  • 22,661
  • 40
  • 140
  • 226
  • Not exactly sure what you asking about. The hide mode you set defines behavior of layout for univisible (setVisible(false)) components. – Eugene Ryzhikov Jan 09 '10 at 00:24
  • @eugener So basically I still code `compXYZ.setVisible(true);` or `compXYZ.setVisible(false);`, and what `hidemode` does is define how the =other= components behave, in terms of layout, when this component's visibility is toggled? – bguiz Jan 09 '10 at 03:17
  • @bquiz I actually defines how layout works with your invisible components. For example "hidemode 3" will think that your invisible component is not even part of layout. – Eugene Ryzhikov Jan 09 '10 at 17:16

2 Answers2

7

The mode applies when the component is validated, as seen in this example. The JFrame's initGUI() method calls pack(), which invokes validate() to do the initial layout. Later, the hideButton's ActionListener calls validate(), which recalculates the container's layout to reflect the new visibility settings. See also, How Layout Management Works.

Community
  • 1
  • 1
trashgod
  • 196,350
  • 25
  • 213
  • 918
  • @trashgod, thanks for linking the example, it was very helpful, however, could you please elaborate on validation? I'm afraid validation wasn't in the example (or I couldn't find it)! – bguiz Jan 09 '10 at 03:24
2

MigLayout's hidemode is useful when you have different views, but only want to display the active one. By default, each view will occupy space in the layout, even if it isn't visible.

Example:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class MigHidemode extends JPanel {

    private class RadioActionListener implements ActionListener {
        public void actionPerformed(final ActionEvent e) {
            final boolean showA = aRadio.isSelected();
            field.setVisible(showA);
            combo.setVisible(!showA);
        }
    }

    public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                    final JDialog dialog = new JDialog();

                    final Container contentPane = dialog.getContentPane();
                    contentPane.setLayout(new BorderLayout());
                    contentPane.add(new MigHidemode(), BorderLayout.CENTER);

                    dialog.addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosed(final WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                    dialog.pack();
                    dialog.setLocationRelativeTo(null);
                    dialog.setVisible(true);
                } catch (final Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private final JTextField field;
    private final JRadioButton aRadio;
    private final JRadioButton bRadio;

    private final JComboBox<String> combo;

    public MigHidemode() {
        setLayout(new MigLayout("", "[]", "[][]"));

        final RadioActionListener radioListener = new RadioActionListener();

        aRadio = new JRadioButton("A");
        aRadio.addActionListener(radioListener);
        add(aRadio, "flowx,cell 0 0");

        bRadio = new JRadioButton("B");
        bRadio.addActionListener(radioListener);
        add(bRadio, "cell 0 0");

        field = new JTextField();
        add(field, "cell 0 1,hidemode 3");
        field.setColumns(20);

        combo = new JComboBox<String>();
        combo.setModel(new DefaultComboBoxModel<String>(new String[] {
            "hello",
            "world" }));
        add(combo, "cell 0 1,hidemode 3");

        final ButtonGroup bg = new ButtonGroup();
        bg.add(aRadio);
        bg.add(bRadio);
        aRadio.doClick();
    }
}
Peter Tseng
  • 11,991
  • 3
  • 64
  • 53