How to implement a JPanel that shows/hide content depending on its width?

I'm trying to implement a JPanel that displays more or less information depending on the available size.

Basically, the idea is have a default content like this:

在这里输入图像描述

That can shrinks to this when the space is reduced:

My code is like this:

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);
} }

I tried to add a ComponentListener and override componentResized() to find when I could show/hide my secondary labels but I was not successful.

Does anybody know how to implement such a behaviour that goes well with MigLayout grow priorities?

Update1: 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?


How about this:

public static JPanel panel(String name) {
    JPanel panel = new JPanel(new MigLayout("insets 0, wrap 4, fillx, debug", "[][][shrink 200][shrink 200, grow 200]"));
    panel.add(new JLabel(name), "spanx 4");
    panel.add(new JLabel("00"));
    panel.add(new JLabel("First lbl"));
    panel.add(new JLabel("01"));
    panel.add(new JLabel("Second lbl"));
    return panel;
}

public static void main(String[] args) {
    JPanel panel = new JPanel(new MigLayout("insets 10, gap 10, fillx, debug"));
    panel.add(panel("Element 1"), "w (50% - 15)!");
    panel.add(panel("Element 2"), "w (50% - 15)!");

    JFrame frame = new JFrame();
    frame.setContentPane(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

I had trouble getting the two main columns to resize equally; I had to do it by setting a width on the components rather than the columns. I'm not sure why that is.


I implemented an autohide mechanism with a LayoutCallback switching the visibility of the component depending on the width of the panel.

Here is an example:

MigLayout layout = new MigLayout("fill");
JPanel panel = new JPanel(layout);

JLabel label1 = new JLabel("Label 1");
label1.putClientProperty("autohide.width", 300);

JLabel label2 = new JLabel("Label 2");
label2.putClientProperty("autohide.width", 200);

panel.add(label1, "grow");
panel.add(label2, "grow");                

layout.addLayoutCallback(new LayoutCallback() {
    @Override
    public void correctBounds(ComponentWrapper wrapper) {
        JComponent component = (JComponent) wrapper.getComponent();

        Number width = (Number) component.getClientProperty("autohide.width");
        if (width != null) {
            if (component.isVisible() && wrapper.getParent().getWidth() < width.intValue()) {
                component.setVisible(false);
            } else if (!component.isVisible() && wrapper.getParent().getWidth() > width.intValue()) {
                component.setVisible(true);
            }
        }
    }
});

Here the label1 is hidden when the width of the panel shrinks below 300 pixels, and label2 disappears when the width is less than 200 pixels.

链接地址: http://www.djcxy.com/p/49966.html

上一篇: 使用自动布局在UICollectionView中指定单元的一个维度

下一篇: 如何实现一个显示/隐藏内容取决于其宽度的JPanel?