title: How to implement a JPanel that shows/hide content depending on its width?
body: <p>I'm trying to implement a JPanel that displays more or less information depending on the available size.</p>
  
  <p>Basically, the idea is have a default content like this:</p>
  
  <p><img src="https://i.stack.imgur.com/EHpIU.jpg" alt="enter image description here"></p>
  
  <p>That can shrinks to this when the space is reduced:</p>
  
  <p><img src="https://i.stack.imgur.com/1pSS5.jpg" alt="enter image description here"></p>
  
  <p>My code is like this:</p>
  
  <pre><code>import net.miginfocom.swing.MigLayout;
  import javax.swing.*;
  
  class PanelDemo extends JPanel {
  private final JLabel title = new JLabel();
  
  private final JLabel counter1 = new JLabel("00");
  private final JLabel counter1Label = new JLabel();
  
  private final JLabel counter2 = new JLabel("00");
  private final JLabel counter2Label = new JLabel();
  
  /**
   * Instantiates a new obs app cadre message bloc panel.
   */
  public PanelDemo() {
      this.setOpaque(false);
      initGUI();
  }
  
  private final void initGUI() {
      // 1°)
      final MigLayout migLayout = new MigLayout(
              "fillx, hidemode 2, debug",
              "[growprio 0][]" //define 4 columns
      );
  
      setLayout(migLayout);
  
      // 2°)
      //
      add(title, "spanx");
      add(counter1, "newline");
      add(counter1Label);
      add(counter2);
      add(counter2Label);
  }
  
  public static void main(String[] args) {
      final JFrame jFrame = new JFrame("test 4");
      jFrame.getContentPane().setLayout(new MigLayout("fillx, debug"));
      final PanelDemo item1 = new PanelDemo();
      item1.title.setText("Element 1");
      item1.counter1Label.setText("First lbl");
      item1.counter2Label.setText("Second lbl");
      jFrame.getContentPane().add(item1, "growx, gpx 110");
  
  
      final PanelDemo item2 = new PanelDemo();
      item2.title.setText("Element 2");
      item2.counter1Label.setText("First lbl");
      item2.counter2Label.setText("Second lbl");
      jFrame.getContentPane().add(item2, "growx, gpx 100");
  
      jFrame.pack();
      jFrame.setVisible(true);
  } }
  </code></pre>
  
  <p>I tried to add a ComponentListener and override componentResized() to find when I could show/hide my secondary labels but I was not successful.</p>
  
  <p>Does anybody know how to implement such a behaviour that goes well with MigLayout grow priorities?</p>
  
  <p><strong>Update1:</strong> I was thinking... what if I set the minimum width to counter1+label1, and the maximum size to counter2+label2 and then listen to resize operations and change the preferred size to either its minimum or its maximum. Would that mecanism work?</p>