
Enabling LC.hideMode() causes layout bug
Hi,
Here is a runnable testcase demonstrating a bug in MigLayout 4.1:
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import net.miginfocom.layout.CC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;
public class Testcase extends JPanel
{
private static final long serialVersionUID = 1L;
private final JToggleButton toggle = new JToggleButton("Toggle");
private final JTree deviceTree;
private final JLabel deviceNameHeader = new JLabel("Device Name");
private final DefaultTreeModel deviceTreeModel;
public Testcase()
{
super(new MigLayout());
final DefaultMutableTreeNode deviceTreeRoot = new DefaultMutableTreeNode("Root");
this.deviceTreeModel = new DefaultTreeModel(deviceTreeRoot);
this.deviceTree = new JTree(deviceTreeModel);
deviceTree.setOpaque(false);
deviceTree.setVisibleRowCount(10);
JScrollPane devicePane = new JScrollPane(deviceTree);
add(devicePane, new CC().push().grow());
JPanel tuningProperties = new JPanel(new MigLayout(new LC().hideMode(3)));
tuningProperties.add(deviceNameHeader);
add(tuningProperties, new CC().grow().wrap());
tuningProperties.add(toggle);
toggle.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (toggle.isSelected())
hideProperties();
else
showProperties();
}
});
}
/**
* Updates the property panel.
* <p/>
* @param node the tree node to update
*/
private void showProperties()
{
deviceNameHeader.setVisible(true);
// Uncommenting the following line fixes the problem
// revalidate();
}
/**
* Invoked when the selected node has no properties.
*/
private void hideProperties()
{
deviceNameHeader.setVisible(false);
// Uncommenting the following line fixes the problem
// revalidate();
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Testcase());
frame.setSize(800, 600);
frame.setVisible(true);
}
}
1. Run testcase
2. Initially you should see "Device Name [Toggle]" in the top right.
3. Click on "toggle".
4. Now you should see [Toggle] in the top right
5. Click on "toggle"
6. Now you should see "Device Name" followed by a thin line which is [Toggle] with incorrect layout
7. Click on "toggle". You should see step 4.
8. Click on "toggle". You should see step 2.
9. Click on "toggle". You should see [Toggle] in the top right, but it has excess spacing on the right.
10. Click on "toggle". You should see step 2. The pattern repeats forever from this point.
Expected behavior: Clicking [Toggle] should toggle between two repeatable states (step 2 and 4). Steps 6 and 9 seem to indicate layout bugs.
Workaround: Uncomment "revalidate()" in the testcase. For some reason this makes suppresses the problem.
Please let me know if you can reproduce this problem on your end.
Thanks,
Gili