Home > Mobile >  MigLayout hidemode is not working and cannot hide component
MigLayout hidemode is not working and cannot hide component

Time:07-11

I have been using MigLayout for a while and i never encountered this problem. For some reason the hidemode constraint is not working and i can't make it hide a button.

Here is a code snippet that demonstrates it:

import java.awt.Container;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import net.miginfocom.swing.MigLayout;

public class Builder extends JFrame {

    private final MigLayout migLayout = new MigLayout("debug, fillx", "[][]", "");

    public Builder() {
        try {
            createAndShowGUI();
        } catch (Throwable th) {
            th.printStackTrace();
            Builder.this.dispatchEvent(new WindowEvent(Builder.this, WindowEvent.WINDOW_CLOSING));
        }
    }

    private void createAndShowGUI() {
        Container container = getContentPane();
        container.setLayout(migLayout);
        JButton b1 = new JButton("b1");
        JButton b2 = new JButton("b2");
        container.add(b1, "hidemode 1, alignx left");
        container.add(b2, "alignx right");
        pack();
        setResizable(true);
        setTitle("Builder");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Builder();
    }
}

I am using version 11.0 of MigLayout

        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout-swing</artifactId>
            <version>11.0</version>
        </dependency>

CodePudding user response:

Hidemode controls how a component is hidden after component.setVisible(false); runs. The component.setVisible(false); is still needed to hide the component. Hidemode controls how surrounding components in the layout are adjusted afterward.

For example, button action listeners could be added to hide and show b1 by calling b1.setVisible(...);

import java.awt.event.WindowEvent;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import net.miginfocom.swing.MigLayout;

public class Builder extends JFrame {

    private final MigLayout migLayout = new MigLayout("debug, fillx", "[][]", "");

    public Builder() {
        try {
            createAndShowGUI();
        } catch (Throwable th) {
            th.printStackTrace();
            Builder.this.dispatchEvent(new WindowEvent(Builder.this, WindowEvent.WINDOW_CLOSING));
        }
    }

    private void createAndShowGUI() {
        Container container = getContentPane();
        container.setLayout(migLayout);
        JButton b1 = new JButton("hide me");
        JButton b2 = new JButton("show it");
        container.add(b1, "hidemode 1, alignx left");
        container.add(b2, "alignx right");

        b1.addActionListener(e -> b1.setVisible(false));
        b2.addActionListener(e -> b1.setVisible(true));

        pack();
        setResizable(true);
        setTitle("Builder");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Builder();
    }
}
  • Related